Hydrotronics

Paintutils for VB

Jan 24th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 11.56 KB | None | 0 0
  1. Public Class paintutils
  2.     Public Shared Function setBackground(ByVal color As Object) As Object
  3.         Console.BackgroundColor = colour
  4.         Console.Clear()
  5.         Return True
  6.     End Function
  7.     Public Shared Function loadImage(ByVal path As String) As Object
  8.         Dim line As String
  9.         Dim data() As String = {}
  10.         Dim choppedData() As String = {}
  11.         Dim lImage() As Integer = {}
  12.         Try
  13.             If Not My.Computer.FileSystem.FileExists(path) Then
  14.                 Error "File does not exist"
  15.             End If
  16.             Using fs As New System.IO.StreamReader(path)
  17.                 line = fs.ReadLine()
  18.                 While line <> "" Or line <> Nothing
  19.                     ReDim Preserve data(data.Length + 2)
  20.                     data(data.Length - 2) = line
  21.                     data(data.Length - 1) = "|"
  22.                     line = fs.ReadLine()
  23.                 End While
  24.             End Using
  25.         Catch ex As Exception
  26.             Console.WriteLine(ex.Message)
  27.         End Try
  28.         For i = 1 To data.Length - 1
  29.             Dim lined As String = data(i)
  30.             Try
  31.                 If lined = "|" Then
  32.                     ReDim Preserve choppedData(choppedData.Length)
  33.                     choppedData(choppedData.Length - 1) = "|"
  34.                 Else
  35.                     For o = 1 To lined.Length
  36.                         ReDim Preserve choppedData(choppedData.Length)
  37.                         choppedData(choppedData.Length - 1) = lined.Substring(o, 1)
  38.                     Next
  39.                 End If
  40.             Catch ex As Exception
  41.  
  42.             End Try
  43.         Next
  44.         For i = 0 To choppedData.Length - 1
  45.             If choppedData(i) <> Nothing Then
  46.                 ReDim Preserve lImage(lImage.Length)
  47.                 If choppedData(i) = " " Then
  48.                     lImage(lImage.Length - 1) = 16
  49.                 ElseIf choppedData(i) = "|" Then
  50.                     lImage(lImage.Length - 1) = 17
  51.                 Else
  52.                     lImage(lImage.Length - 1) = Convert.ToInt64(choppedData(i), 16)
  53.                 End If
  54.             End If
  55.         Next
  56.         Return lImage
  57.     End Function
  58.     Public Shared Function drawImage(ByVal image As Object, ByVal x As Integer, ByVal y As Integer)
  59.         Dim lines As Integer = 0
  60.         Dim pos As Integer = 0
  61.         If Not IsArray(image) Then
  62.             Error "Array expected"
  63.         End If
  64.         For i = 0 To image.length - 1
  65.             If image(i) <> 17 And image(i) = 16 Then
  66.                 pos += 1
  67.             ElseIf image(i) <> 17 Then
  68.                 paintutils.drawPixel(x + pos, y + lines, image(i))
  69.                 pos += 1
  70.             Else
  71.                 pos = 0
  72.                 lines += 1
  73.             End If
  74.         Next
  75.         Return True
  76.     End Function
  77.     Public Shared Function drawPixel(ByVal x As Integer, ByVal y As Integer) As Object
  78.         If x < 0 or x > Console.WindowWidth - 1 or y < 0 or x > Console.WindowHeight - 1 Then
  79.             Return False
  80.             Exit Function
  81.         End If
  82.         x = Math.Floor(x)
  83.         y = Math.Floor(y)
  84.         Console.BackgroundColor = ConsoleColor.White
  85.         Console.SetCursorPosition(x - 1, y - 1)
  86.         Console.Write(" ")
  87.         Return True
  88.     End Function
  89.     Public Shared Function drawPixel(ByVal x As Integer, ByVal y As Integer, ByVal colour As Object) As Object
  90.         If x < 0 or x > Console.WindowWidth - 1 or y < 0 or x > Console.WindowHeight - 1 Then
  91.             Return False
  92.             Exit Function
  93.         End If
  94.         x = Math.Floor(x)
  95.         y = Math.Floor(y)
  96.         Console.BackgroundColor = colour
  97.         Console.SetCursorPosition(x, y)
  98.         Console.Write(" ")
  99.         Return True
  100.     End Function
  101.     Public Shared Function drawLine(ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer) As Object
  102.         x1 = Math.Floor(x1)
  103.         y1 = Math.Floor(y1)
  104.         x2 = Math.Floor(x2)
  105.         y2 = Math.Floor(y2)
  106.  
  107.         If x1 = x2 And y1 = y2 Then
  108.             paintutils.drawPixel(x1, y1)
  109.             Return True
  110.             Exit Function
  111.         End If
  112.  
  113.         Dim minX As Integer = Math.Min(x1, x2)
  114.         Dim minY As Integer
  115.         Dim maxX As Integer
  116.         Dim maxY As Integer
  117.         If minX = x1 Then
  118.             minY = y1
  119.             maxX = x2
  120.             maxY = y2
  121.         Else
  122.             minY = y2
  123.             maxX = x1
  124.             maxY = x2
  125.         End If
  126.  
  127.         Dim xDiff As Integer = maxX - minX
  128.         Dim yDiff As Integer = maxY - minY
  129.  
  130.         If xDiff > Math.Abs(yDiff) Then
  131.             Dim y As Integer = minY
  132.             Dim dyy As Decimal = yDiff / xDiff
  133.             For x = minX To maxX
  134.                 paintutils.drawPixel(x, Math.Floor(y + 0.5))
  135.                 y = y + dyy
  136.             Next
  137.         Else
  138.             Dim x As Integer = minX
  139.             Dim dxx As Decimal = xDiff / yDiff
  140.             If maxY >= minY Then
  141.                 For y = minY To maxY
  142.                     paintutils.drawPixel(Math.Floor(x + 0.5), y)
  143.                     x = x + dxx
  144.                 Next
  145.             Else
  146.                 For y = minY To maxY Step -1
  147.                     paintutils.drawPixel(Math.Floor(x + 0.5), y)
  148.                     x = x - dxx
  149.                 Next
  150.             End If
  151.         End If
  152.         Return True
  153.     End Function
  154.     Public Shared Function drawLine(ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer, ByVal colour As Object) As Object
  155.         x1 = Math.Floor(x1)
  156.         y1 = Math.Floor(y1)
  157.         x2 = Math.Floor(x2)
  158.         y2 = Math.Floor(y2)
  159.  
  160.         If x1 = x2 And y1 = y2 Then
  161.             paintutils.drawPixel(x1, y1)
  162.             Return True
  163.             Exit Function
  164.         End If
  165.  
  166.         Dim minX As Integer = Math.Min(x1, x2)
  167.         Dim minY As Integer
  168.         Dim maxX As Integer
  169.         Dim maxY As Integer
  170.         If minX = x1 Then
  171.             minY = y1
  172.             maxX = x2
  173.             maxY = y2
  174.         Else
  175.             minY = y2
  176.             maxX = x1
  177.             maxY = x2
  178.         End If
  179.  
  180.         Dim xDiff As Integer = maxX - minX
  181.         Dim yDiff As Integer = maxY - minY
  182.  
  183.         If xDiff > Math.Abs(yDiff) Then
  184.             Dim y As Integer = minY
  185.             Dim dyy As Decimal = yDiff / xDiff
  186.             For x = minX To maxX
  187.                 paintutils.drawPixel(x, Math.Floor(y + 0.5), colour)
  188.                 y = y + dyy
  189.             Next
  190.         Else
  191.             Dim x As Integer = minX
  192.             Dim dxx As Decimal = xDiff / yDiff
  193.             If maxY >= minY Then
  194.                 For y = minY To maxY
  195.                     paintutils.drawPixel(Math.Floor(x + 0.5), y, colour)
  196.                     x = x + dxx
  197.                 Next
  198.             Else
  199.                 For y = minY To maxY Step -1
  200.                     paintutils.drawPixel(Math.Floor(x + 0.5), y, colour)
  201.                     x = x - dxx
  202.                 Next
  203.             End If
  204.         End If
  205.         Return True
  206.     End Function
  207.     Public Shared Function setTextColour(ByVal colour As Object)
  208.         Console.ForegroundColor = colour
  209.         Return True
  210.     End Function
  211.     Public Shared Function setTextColor(ByVal color As Object)
  212.         Console.ForegroundColor = color
  213.         Return True
  214.     End Function
  215.     Public Shared Function setBackgroundColour(ByVal colour As Object) As Object
  216.         Console.BackgroundColor = colour
  217.         Return True
  218.     End Function
  219.     Public Shared Function setBackgroundColor(ByVal color As Object) As Object
  220.         Console.BackgroundColor = color
  221.         Return True
  222.     End Function
  223.     Public Shared Function drawBox(ByVal x1 As Decimal, ByVal y1 As Decimal, ByVal x2 As Decimal, ByVal y2 As Decimal, ByVal colour As Object) As Object
  224.         x1 = Math.Floor(x1)
  225.         y1 = Math.Floor(y1)
  226.         x2 = Math.Floor(x2)
  227.         y2 = Math.Floor(y2)
  228.  
  229.         If x1 = x2 And y1 = y2 Then
  230.             paintutils.drawPixel(x1, y2, colour)
  231.             Return True
  232.             Exit Function
  233.         End If
  234.  
  235.         Dim minX As Integer = Math.Min(x1, x2)
  236.         Dim minY As Integer
  237.         Dim maxX As Integer
  238.         Dim maxY As Integer
  239.         If minX = x1 Then
  240.             minY = y1
  241.             maxX = x2
  242.             maxY = y2
  243.         Else
  244.             minY = y2
  245.             maxX = x1
  246.             maxY = y1
  247.         End If
  248.  
  249.         For x = minX To maxX
  250.             paintutils.drawPixel(x, minY, colour)
  251.             paintutils.drawPixel(x, maxY, colour)
  252.         Next
  253.  
  254.         If (maxY - minY) >= 2 Then
  255.             For y = (minY + 1) To (maxY - 1)
  256.                 paintutils.drawPixel(minX, y, colour)
  257.                 paintutils.drawPixel(maxX, y, colour)
  258.             Next
  259.         End If
  260.         Return True
  261.     End Function
  262.     Public Shared Function drawBox(ByVal x1 As Decimal, ByVal y1 As Decimal, ByVal x2 As Decimal, ByVal y2 As Decimal) As Object
  263.         x1 = Math.Floor(x1)
  264.         y1 = Math.Floor(y1)
  265.         x2 = Math.Floor(x2)
  266.         y2 = Math.Floor(y2)
  267.  
  268.         If x1 = x2 And y1 = y2 Then
  269.             paintutils.drawPixel(x1, y2)
  270.             Return True
  271.             Exit Function
  272.         End If
  273.  
  274.         Dim minX As Integer = Math.Min(x1, x2)
  275.         Dim minY As Integer
  276.         Dim maxX As Integer
  277.         Dim maxY As Integer
  278.         If minX = x1 Then
  279.             minY = y1
  280.             maxX = x2
  281.             maxY = y2
  282.         Else
  283.             minY = y2
  284.             maxX = x1
  285.             maxY = y1
  286.         End If
  287.  
  288.         For x = minX To maxX
  289.             paintutils.drawPixel(x, minY)
  290.             paintutils.drawPixel(x, maxY)
  291.         Next
  292.  
  293.         If (maxY - minY) >= 2 Then
  294.             For y = (minY + 1) To (maxY - 1)
  295.                 paintutils.drawPixel(minX, y)
  296.                 paintutils.drawPixel(maxX, y)
  297.             Next
  298.         End If
  299.         Return True
  300.     End Function
  301.     Public Shared Function drawFilledBox(ByVal x1 As Decimal, ByVal y1 As Decimal, ByVal x2 As Decimal, ByVal y2 As Decimal, ByVal colour As Object) As Object
  302.         x1 = Math.Floor(x1)
  303.         y1 = Math.Floor(y1)
  304.         x2 = Math.Floor(x2)
  305.         y2 = Math.Floor(y2)
  306.  
  307.         If x1 = x2 And y1 = y2 Then
  308.             paintutils.drawPixel(x1, y2, colour)
  309.             Return True
  310.             Exit Function
  311.         End If
  312.  
  313.         Dim minX As Integer = Math.Min(x1, x2)
  314.         Dim minY As Integer
  315.         Dim maxX As Integer
  316.         Dim maxY As Integer
  317.         If minX = x1 Then
  318.             minY = y1
  319.             maxX = x2
  320.             maxY = y2
  321.         Else
  322.             minY = y2
  323.             maxX = x1
  324.             maxY = y1
  325.         End If
  326.  
  327.         For x = minX To maxX
  328.             For y = minY To maxY
  329.                 paintutils.drawPixel(x, y, colour)
  330.             Next
  331.         Next
  332.         Return True
  333.     End Function
  334.     Public Shared Function drawFilledBox(ByVal x1 As Decimal, ByVal y1 As Decimal, ByVal x2 As Decimal, ByVal y2 As Decimal) As Object
  335.         x1 = Math.Floor(x1)
  336.         y1 = Math.Floor(y1)
  337.         x2 = Math.Floor(x2)
  338.         y2 = Math.Floor(y2)
  339.  
  340.         If x1 = x2 And y1 = y2 Then
  341.             paintutils.drawPixel(x1, y2)
  342.             Return True
  343.             Exit Function
  344.         End If
  345.  
  346.         Dim minX As Integer = Math.Min(x1, x2)
  347.         Dim minY As Integer
  348.         Dim maxX As Integer
  349.         Dim maxY As Integer
  350.         If minX = x1 Then
  351.             minY = y1
  352.             maxX = x2
  353.             maxY = y2
  354.         Else
  355.             minY = y2
  356.             maxX = x1
  357.             maxY = y1
  358.         End If
  359.  
  360.         For x = minX To maxX
  361.             For y = minY To maxY
  362.                 paintutils.drawPixel(x, y)
  363.             Next
  364.         Next
  365.         Return True
  366.     End Function
  367. End Class
Add Comment
Please, Sign In to add comment