Advertisement
Guest User

Untitled

a guest
Dec 6th, 2012
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit On
  2. Imports System.IO
  3. Imports DRAWER.DoublePoint
  4.  
  5. Public Class Form1
  6.  
  7.     Dim MainImage As New DynamicBitmap 'the image we can draw on
  8.  
  9.     ' our points
  10.    Dim Points() As DPoint =
  11.     {
  12.         New DPoint(0.0, 0.0),
  13.         New DPoint(100.0, 100.0),
  14.         New DPoint(100.0, 150.0),
  15.         New DPoint(150.0, 150.0),
  16.         New DPoint(200.0, -200.0),
  17.         New DPoint(-200.0, -200.0),
  18.         New DPoint(0.0, -100.0),
  19.         New DPoint(-100.0, -100.0),
  20.         New DPoint(-100.0, 0.0),
  21.         New DPoint(-50.0, 50.0),
  22.         New DPoint(-100.0, 200.0),
  23.         New DPoint(-180.0, 220.0),
  24.         New DPoint(250.0, 250.0)
  25.     } ' our points
  26.  
  27.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  28.         Dim temp As New DynamicBitmap ' temporary image
  29.        temp.LoadBitmap("map.jpg") 'load map.jpg from working directory
  30.  
  31.         MainImage.CreateGrid(500, 500, 1, 1) 'create a 500x500 grid, each 1 by 1 pixel
  32.        MainImage.DrawOnSurface(temp.Bitmap, temp.Rectangle, MainImage.Rectangle) 'draw temp onto mainimage
  33.  
  34.         MainImage.Surface.DrawLine(Pens.Black, 0, 250, 500, 250) 'draw Y axis from (0,250) to (500,250) <- image, not "XY-system", coordinates!1!1!
  35.        MainImage.Surface.DrawLine(Pens.Black, 250, 0, 250, 500) 'draw X axis ""
  36.  
  37.         For i As Integer = 0 To Points.Length - 2 'create all the lines from Points()
  38.            MainImage.Surface.DrawLine(Pens.Black, PointToImage(Points(i).ToInt()), PointToImage(Points(i + 1).ToInt()))
  39.         Next
  40.  
  41.         PictureBox1.Image = MainImage.Bitmap 'assign mainimage to picturebox
  42.        PictureBox1.Refresh() ' refresh picturebox
  43.    End Sub
  44.  
  45.     'BEGIN--------------ANSWER 2-----------------------
  46.    Function func_u(ByVal P As DPoint) As DPoint 'function u((x,y))
  47.        Return P / ((P.X * P.X) + (P.Y * P.Y)) ' (1)
  48.    End Function
  49.     Function func_r(ByVal P As DPoint) As DPoint 'function r((x,y))
  50.        Return func_u(New DPoint(P.Y, -P.X)) ' (2)
  51.    End Function
  52.  
  53.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  54.         Dim P() As DPoint = Points 'aquire our list of points into the array P
  55.        Dim B(0 To 1)() As DPoint 'declare our "purple" points array, (0) will be the + ones and (1) the - ones
  56.        Dim distance As Double = 5.0 ' distance we want
  57.        For k As Integer = 1 To P.Length - 2 'loop from k = 1 to k = amount of Points()-1
  58.            ReDim Preserve B(0)(k - 1) 'just allocation stuff for the program memory
  59.            ReDim Preserve B(1)(k - 1) 'just allocation stuff for the program memory
  60.            B(0)(k - 1) = P(k) + ((func_r(func_u(P(k + 1) - P(k)) - func_u(P(k - 1) - P(k)))) * distance) 'calculation +(3)
  61.            B(1)(k - 1) = P(k) - ((func_r(func_u(P(k + 1) - P(k)) - func_u(P(k - 1) - P(k)))) * distance) 'calculation -(3)
  62.        Next
  63.         'skipping (4) and (5) because for now we just need the purple points around the lines
  64.        DrawCross(B(0), Pens.Red) 'draw all points in B(0) -> +(3) on the image
  65.        DrawCross(B(1), Pens.Blue) 'draw all points in B(1) -> -(3) on the image
  66.        PictureBox1.Refresh() 'refresh our picture
  67.    End Sub
  68.     'END---------------ANSWER 2-----------------------
  69.  
  70.     'BEGIN--------------ANSWER 1-----------------------
  71.    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  72.         Dim B() As DPoint = Points
  73.         Dim len As Integer = 0
  74.         len = (B.Length - 2)
  75.         Dim u(len) As DPoint
  76.         len = (u.Length - 2)
  77.         Dim bs(len) As DPoint
  78.         Dim n(len) As DPoint
  79.  
  80.         Dim dist As Double = 10.0
  81.         Dim PL(len) As DPoint
  82.         Dim PR(len) As DPoint
  83.         Dim P((PL.Length + PR.Length) - 1) As DPoint
  84.         For i As Integer = 0 To u.Length - 1 '1.Determine each u(k)
  85.            u(i) = B(i + 1) - B(i)
  86.         Next
  87.         For i As Integer = 0 To bs.Length - 1 '2.Determine each bisector b(k)   and unit bisector n(k)
  88.            bs(i) = New DPoint(-(u(i) + u(i + 1)).Y, (u(i) + u(i + 1)).X)
  89.             Dim divider As Double = ((bs(i).X * bs(i).X) + (bs(i).Y * bs(i).Y))
  90.             n(i) = bs(i) / Math.Sqrt(divider)
  91.         Next
  92.         For i As Integer = 0 To PL.Length - 1 '3.Determine the points P′ and P′′. | ′ = 0, ′′ = 1
  93.            PL(i) = B(i + 1) + (n(i) * dist)
  94.             PR(i) = B(i + 1) - (n(i) * dist)
  95.             P(i) = PL(i)
  96.         Next
  97.         For i As Integer = 0 To PL.Length - 1 'combine PL and PR
  98.            P(PL.Length + i) = PR((PL.Length - 1) - i)
  99.         Next
  100.  
  101.         Dim Polygon(P.Length + 2) As DPoint
  102.         Polygon(0) = B(0)
  103.         For i As Integer = 0 To PL.Length - 1
  104.             Polygon(i + 1) = P(i)
  105.         Next
  106.         Polygon(PL.Length + 1) = B(B.Length - 1)
  107.         For i As Integer = 0 To PL.Length - 1
  108.             Polygon(PL.Length + i + 2) = P(PL.Length + i)
  109.         Next
  110.         Polygon(Polygon.Length - 1) = B(0)
  111.  
  112.         For i As Integer = 0 To Polygon.Length - 2
  113.             MainImage.Surface.DrawLine(Pens.Red, PointToImage(Polygon(i).ToInt()), PointToImage(Polygon(i + 1).ToInt()))
  114.         Next
  115.         MainImage.Surface.DrawLine(Pens.Red, PointToImage(Polygon(Polygon.Length - 1).ToInt()), PointToImage(Polygon(0).ToInt()))
  116.  
  117.         PictureBox1.Image = MainImage.Bitmap
  118.         PictureBox1.Refresh()
  119.     End Sub
  120.     'END--------------ANSWER 1-----------------------
  121.  
  122.     Public Sub DrawCross(ByVal Point As Point, ByVal color As Pen)
  123.         ' draw a cross on the main image at a given point
  124.        MainImage.Surface.DrawLine(color, Point.X - 1, Point.Y - 1, Point.X + 1, Point.Y + 1)
  125.         MainImage.Surface.DrawLine(color, Point.X + 1, Point.Y - 1, Point.X - 1, Point.Y + 1)
  126.     End Sub
  127.     Public Sub DrawCross(ByVal Point() As DPoint, ByVal color As Pen)
  128.         ' same as DrawCross but takes an array of points as input
  129.        For i As Integer = 0 To Point.Length - 1
  130.             MainImage.Surface.DrawLine(color, PointToImage(Point(i) - 1.0), PointToImage(Point(i) + 1.0))
  131.             MainImage.Surface.DrawLine(color, PointToImage(Point(i) + New DPoint(1.0, -1.0)), PointToImage(Point(i) + New DPoint(-1.0, 1.0)))
  132.         Next
  133.     End Sub
  134. End Class
  135. ''---------------end of answer results-----------------------
  136. ''---------------Dpoint structure here-----------------------
  137. Public Class DoublePoint
  138.     Public Structure DPoint
  139.         Dim X As Double
  140.         Dim Y As Double
  141.         Sub New(ByVal pX As Double, ByVal pY As Double)
  142.             X = pX
  143.             Y = pY
  144.         End Sub
  145.         Public Function ToInt()
  146.             Return New Point(CInt(Math.Round(X)), CInt(Math.Round(Y)))
  147.         End Function
  148.         Public Shared Operator <>(ByVal a As DPoint, ByVal b As DPoint) As Boolean
  149.             If a.X <> b.X Then Return False
  150.             If a.Y <> b.Y Then Return False
  151.             Return True
  152.         End Operator
  153.         Public Shared Operator =(ByVal a As DPoint, ByVal b As DPoint) As Boolean
  154.             If a.X <> b.X Then Return False
  155.             If a.Y <> b.Y Then Return False
  156.             Return True
  157.         End Operator
  158.         Public Shared Operator +(ByVal a As DPoint, ByVal b As DPoint) As DPoint
  159.             Return New DPoint(a.X + b.X, a.Y + b.Y)
  160.         End Operator
  161.         Public Shared Operator -(ByVal a As DPoint, ByVal b As DPoint) As DPoint
  162.             Return New DPoint(a.X - b.X, a.Y - b.Y)
  163.         End Operator
  164.         Public Shared Operator *(ByVal a As DPoint, ByVal b As DPoint) As DPoint
  165.             Return New DPoint(a.X * b.X, a.Y * b.Y)
  166.         End Operator
  167.         Public Shared Operator /(ByVal a As DPoint, ByVal b As DPoint) As DPoint
  168.             Return New DPoint(a.X / b.X, a.Y / b.Y)
  169.         End Operator
  170.         Public Shared Operator /(ByVal a As DPoint, ByVal b As Double) As DPoint
  171.             Return New DPoint(a.X / b, a.Y / b)
  172.         End Operator
  173.         Public Shared Operator *(ByVal a As DPoint, ByVal b As Double) As DPoint
  174.             Return New DPoint(a.X * b, a.Y * b)
  175.         End Operator
  176.         Public Shared Operator +(ByVal a As DPoint, ByVal b As Double) As DPoint
  177.             Return New DPoint(a.X + b, a.Y + b)
  178.         End Operator
  179.         Public Shared Operator -(ByVal a As DPoint, ByVal b As Double) As DPoint
  180.             Return New DPoint(a.X - b, a.Y - b)
  181.         End Operator
  182.     End Structure
  183.     Public Shared Function PointToImage(ByVal P1 As Point) As Point
  184.         ' convert XY coordinate points to image points, image (0,0) is at top left corner
  185.        '(in a coordinate system it's the middle point)
  186.        'and middle at (250,250) while the middle on an XY system equals (0,0), so we convert that here:
  187.        Return New Point(250 + P1.X, 250 - P1.Y)
  188.     End Function
  189.     Public Shared Function PointToImage(ByVal P1() As Point) As Point()
  190.         'this point to image variant takes a whole array of points instead of just one point
  191.        Dim ret(0 To P1.Length - 1) As Point
  192.         For i As Integer = 0 To P1.Length - 1
  193.             ret(i) = PointToImage(P1(i))
  194.         Next
  195.         Return ret
  196.     End Function
  197.     Public Shared Function PointToImage(ByVal P1 As DPoint) As Point
  198.         'special variation for the Dpoint type
  199.        Return New Point(250 + CInt(P1.X), 250 - CInt(P1.Y))
  200.     End Function
  201.     Public Shared Function PointToImage(ByVal P1() As DPoint) As Point()
  202.         'special variation for the Dpoint type
  203.        Dim ret(0 To P1.Length - 1) As Point
  204.         For i As Integer = 0 To P1.Length - 1
  205.             ret(i) = PointToImage(P1(i))
  206.         Next
  207.         Return ret
  208.     End Function
  209. End Class
  210.  
  211. ''---------------end of my code-----------------------
  212. ''bitmap class for drawing here, nothing "important"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement