Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Explicit On
- Imports System.IO
- Imports DRAWER.DoublePoint
- Public Class Form1
- Dim MainImage As New DynamicBitmap 'the image we can draw on
- ' our points
- Dim Points() As DPoint =
- {
- New DPoint(0.0, 0.0),
- New DPoint(100.0, 100.0),
- New DPoint(100.0, 150.0),
- New DPoint(150.0, 150.0),
- New DPoint(200.0, -200.0),
- New DPoint(-200.0, -200.0),
- New DPoint(0.0, -100.0),
- New DPoint(-100.0, -100.0),
- New DPoint(-100.0, 0.0),
- New DPoint(-50.0, 50.0),
- New DPoint(-100.0, 200.0),
- New DPoint(-180.0, 220.0),
- New DPoint(250.0, 250.0)
- } ' our points
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- Dim temp As New DynamicBitmap ' temporary image
- temp.LoadBitmap("map.jpg") 'load map.jpg from working directory
- MainImage.CreateGrid(500, 500, 1, 1) 'create a 500x500 grid, each 1 by 1 pixel
- MainImage.DrawOnSurface(temp.Bitmap, temp.Rectangle, MainImage.Rectangle) 'draw temp onto mainimage
- 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!
- MainImage.Surface.DrawLine(Pens.Black, 250, 0, 250, 500) 'draw X axis ""
- For i As Integer = 0 To Points.Length - 2 'create all the lines from Points()
- MainImage.Surface.DrawLine(Pens.Black, PointToImage(Points(i).ToInt()), PointToImage(Points(i + 1).ToInt()))
- Next
- PictureBox1.Image = MainImage.Bitmap 'assign mainimage to picturebox
- PictureBox1.Refresh() ' refresh picturebox
- End Sub
- 'BEGIN--------------ANSWER 2-----------------------
- Function func_u(ByVal P As DPoint) As DPoint 'function u((x,y))
- Return P / ((P.X * P.X) + (P.Y * P.Y)) ' (1)
- End Function
- Function func_r(ByVal P As DPoint) As DPoint 'function r((x,y))
- Return func_u(New DPoint(P.Y, -P.X)) ' (2)
- End Function
- Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
- Dim P() As DPoint = Points 'aquire our list of points into the array P
- Dim B(0 To 1)() As DPoint 'declare our "purple" points array, (0) will be the + ones and (1) the - ones
- Dim distance As Double = 5.0 ' distance we want
- For k As Integer = 1 To P.Length - 2 'loop from k = 1 to k = amount of Points()-1
- ReDim Preserve B(0)(k - 1) 'just allocation stuff for the program memory
- ReDim Preserve B(1)(k - 1) 'just allocation stuff for the program memory
- 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)
- 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)
- Next
- 'skipping (4) and (5) because for now we just need the purple points around the lines
- DrawCross(B(0), Pens.Red) 'draw all points in B(0) -> +(3) on the image
- DrawCross(B(1), Pens.Blue) 'draw all points in B(1) -> -(3) on the image
- PictureBox1.Refresh() 'refresh our picture
- End Sub
- 'END---------------ANSWER 2-----------------------
- 'BEGIN--------------ANSWER 1-----------------------
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim B() As DPoint = Points
- Dim len As Integer = 0
- len = (B.Length - 2)
- Dim u(len) As DPoint
- len = (u.Length - 2)
- Dim bs(len) As DPoint
- Dim n(len) As DPoint
- Dim dist As Double = 10.0
- Dim PL(len) As DPoint
- Dim PR(len) As DPoint
- Dim P((PL.Length + PR.Length) - 1) As DPoint
- For i As Integer = 0 To u.Length - 1 '1.Determine each u(k)
- u(i) = B(i + 1) - B(i)
- Next
- For i As Integer = 0 To bs.Length - 1 '2.Determine each bisector b(k) and unit bisector n(k)
- bs(i) = New DPoint(-(u(i) + u(i + 1)).Y, (u(i) + u(i + 1)).X)
- Dim divider As Double = ((bs(i).X * bs(i).X) + (bs(i).Y * bs(i).Y))
- n(i) = bs(i) / Math.Sqrt(divider)
- Next
- For i As Integer = 0 To PL.Length - 1 '3.Determine the points P′ and P′′. | ′ = 0, ′′ = 1
- PL(i) = B(i + 1) + (n(i) * dist)
- PR(i) = B(i + 1) - (n(i) * dist)
- P(i) = PL(i)
- Next
- For i As Integer = 0 To PL.Length - 1 'combine PL and PR
- P(PL.Length + i) = PR((PL.Length - 1) - i)
- Next
- Dim Polygon(P.Length + 2) As DPoint
- Polygon(0) = B(0)
- For i As Integer = 0 To PL.Length - 1
- Polygon(i + 1) = P(i)
- Next
- Polygon(PL.Length + 1) = B(B.Length - 1)
- For i As Integer = 0 To PL.Length - 1
- Polygon(PL.Length + i + 2) = P(PL.Length + i)
- Next
- Polygon(Polygon.Length - 1) = B(0)
- For i As Integer = 0 To Polygon.Length - 2
- MainImage.Surface.DrawLine(Pens.Red, PointToImage(Polygon(i).ToInt()), PointToImage(Polygon(i + 1).ToInt()))
- Next
- MainImage.Surface.DrawLine(Pens.Red, PointToImage(Polygon(Polygon.Length - 1).ToInt()), PointToImage(Polygon(0).ToInt()))
- PictureBox1.Image = MainImage.Bitmap
- PictureBox1.Refresh()
- End Sub
- 'END--------------ANSWER 1-----------------------
- Public Sub DrawCross(ByVal Point As Point, ByVal color As Pen)
- ' draw a cross on the main image at a given point
- MainImage.Surface.DrawLine(color, Point.X - 1, Point.Y - 1, Point.X + 1, Point.Y + 1)
- MainImage.Surface.DrawLine(color, Point.X + 1, Point.Y - 1, Point.X - 1, Point.Y + 1)
- End Sub
- Public Sub DrawCross(ByVal Point() As DPoint, ByVal color As Pen)
- ' same as DrawCross but takes an array of points as input
- For i As Integer = 0 To Point.Length - 1
- MainImage.Surface.DrawLine(color, PointToImage(Point(i) - 1.0), PointToImage(Point(i) + 1.0))
- MainImage.Surface.DrawLine(color, PointToImage(Point(i) + New DPoint(1.0, -1.0)), PointToImage(Point(i) + New DPoint(-1.0, 1.0)))
- Next
- End Sub
- End Class
- ''---------------end of answer results-----------------------
- ''---------------Dpoint structure here-----------------------
- Public Class DoublePoint
- Public Structure DPoint
- Dim X As Double
- Dim Y As Double
- Sub New(ByVal pX As Double, ByVal pY As Double)
- X = pX
- Y = pY
- End Sub
- Public Function ToInt()
- Return New Point(CInt(Math.Round(X)), CInt(Math.Round(Y)))
- End Function
- Public Shared Operator <>(ByVal a As DPoint, ByVal b As DPoint) As Boolean
- If a.X <> b.X Then Return False
- If a.Y <> b.Y Then Return False
- Return True
- End Operator
- Public Shared Operator =(ByVal a As DPoint, ByVal b As DPoint) As Boolean
- If a.X <> b.X Then Return False
- If a.Y <> b.Y Then Return False
- Return True
- End Operator
- Public Shared Operator +(ByVal a As DPoint, ByVal b As DPoint) As DPoint
- Return New DPoint(a.X + b.X, a.Y + b.Y)
- End Operator
- Public Shared Operator -(ByVal a As DPoint, ByVal b As DPoint) As DPoint
- Return New DPoint(a.X - b.X, a.Y - b.Y)
- End Operator
- Public Shared Operator *(ByVal a As DPoint, ByVal b As DPoint) As DPoint
- Return New DPoint(a.X * b.X, a.Y * b.Y)
- End Operator
- Public Shared Operator /(ByVal a As DPoint, ByVal b As DPoint) As DPoint
- Return New DPoint(a.X / b.X, a.Y / b.Y)
- End Operator
- Public Shared Operator /(ByVal a As DPoint, ByVal b As Double) As DPoint
- Return New DPoint(a.X / b, a.Y / b)
- End Operator
- Public Shared Operator *(ByVal a As DPoint, ByVal b As Double) As DPoint
- Return New DPoint(a.X * b, a.Y * b)
- End Operator
- Public Shared Operator +(ByVal a As DPoint, ByVal b As Double) As DPoint
- Return New DPoint(a.X + b, a.Y + b)
- End Operator
- Public Shared Operator -(ByVal a As DPoint, ByVal b As Double) As DPoint
- Return New DPoint(a.X - b, a.Y - b)
- End Operator
- End Structure
- Public Shared Function PointToImage(ByVal P1 As Point) As Point
- ' convert XY coordinate points to image points, image (0,0) is at top left corner
- '(in a coordinate system it's the middle point)
- 'and middle at (250,250) while the middle on an XY system equals (0,0), so we convert that here:
- Return New Point(250 + P1.X, 250 - P1.Y)
- End Function
- Public Shared Function PointToImage(ByVal P1() As Point) As Point()
- 'this point to image variant takes a whole array of points instead of just one point
- Dim ret(0 To P1.Length - 1) As Point
- For i As Integer = 0 To P1.Length - 1
- ret(i) = PointToImage(P1(i))
- Next
- Return ret
- End Function
- Public Shared Function PointToImage(ByVal P1 As DPoint) As Point
- 'special variation for the Dpoint type
- Return New Point(250 + CInt(P1.X), 250 - CInt(P1.Y))
- End Function
- Public Shared Function PointToImage(ByVal P1() As DPoint) As Point()
- 'special variation for the Dpoint type
- Dim ret(0 To P1.Length - 1) As Point
- For i As Integer = 0 To P1.Length - 1
- ret(i) = PointToImage(P1(i))
- Next
- Return ret
- End Function
- End Class
- ''---------------end of my code-----------------------
- ''bitmap class for drawing here, nothing "important"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement