I have a VB.NET program that draws a triangle every 'tick' from some given info. This code was adapted from some VB6 code on Lucky's VB site from a tutorial called CarAI. Here is the VB.NET code:
'/////////////////////////////////////////////////////////////////
Dim intX1 As Integer 'Coordinates of the 3 triangle verticies
Dim intY1 As Integer
Dim intX2 As Integer
Dim intY2 As Integer
Dim intX3 As Integer
Dim intY3 As Integer
Dim p As New Pen(Color.Blue, 1)
intX1 = Location_X + Radius * Math.Sin(Direction)
intY1 = Location_Y - Radius * Math.Cos(Direction)
intX2 = Location_X + Radius * Math.Sin(Direction + 2 * PI / 3)
intY2 = Location_Y - Radius * Math.Cos(Direction + 2 * PI / 3)
intX3 = Location_X + Radius * Math.Sin(Direction + 4 * PI / 3)
intY3 = Location_Y - Radius * Math.Cos(Direction + 4 * PI / 3)
g.DrawLine(p, intX1, intY1, intX2, intY2)
g.DrawLine(p, intX2, intY2, intX3, intY3)
g.DrawLine(p, intX3, intY3, intX1, intY1)
'/////////////////////////////////////////////////////////////////
I believe radius just changes the size of the triangle.
My question is, how do I modify this code to draw 4 lines making up a rectangle(instead of a triangle) of a given length and width? I do not understand this math very well and really have no idea how to do it.
Thank you
Const POLYGONSIDES As Integer = 4 ' change number of sides for your polygon
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.Paint
Dim g As Graphics
Dim myPts() As Point ' create an array of points
Dim SideAngle As Single
Dim X, Y, i As Integer
Dim p As New Pen(Color.Blue, 1)
ReDim myPts(POLYGONSIDES - 1)
SideAngle = 2 * Math.PI / POLYGONSIDES
For i = 0 To POLYGONSIDES - 1
X = Location_X + Radius * Math.Sin(Direction + i * SideAngle)
Y = Location_Y - Radius * Math.Cos(Direction + i * SideAngle)
myPts(i) = New Point(X, Y)
Next i
g = e.Graphics
g.DrawPolygon(p, myPts)
End Sub
The SideAngle variable in the previous example represents the angle within a cirle that represents the chord created by one side. The SideAngle is calculated by taking the radian angles for a circle (2*PI) and dividing it by the number of sides. Radius holds the length of the radius of the circle in which the polygon is inscribed. The Direction variable represents the base angle for the first point on the polygon. The subsequent points are found by adding a multiple of the unit angle (SideAngle).
Drawing a rectangle of varying width and height is going to be a little more complicated than the equilateral triangle.
The following code is from a rectangle class that I created. To draw the rectangle, I first find the center by dividing Width and Height by 2.
Public Sub DrawRect(ByVal g As Graphics)
Dim RectPts(3) As Point
Dim Theta As Single
Dim HalfWid, HalfHgt As Integer
HalfWid = Width / 2
HalfHgt = Height / 2
Rad = Math.Sqrt(HalfWid * HalfWid + HalfHgt * HalfHgt)
Theta = Math.Acos(HalfWid / Rad)
RectPts(0) = GetPt(Theta)
RectPts(1) = GetPt(Math.PI - Theta)
RectPts(2) = GetPt(Math.PI + Theta)
RectPts(3) = GetPt(-Theta)
g.DrawPolygon(PenColor, RectPts)
End Sub
Private Function GetPt(ByVal Offset As Single) As Point
Dim tAngle As Single
tAngle = Rotation / 180 * Math.PI + Offset
GetPt.X = CenterX + Rad * Math.Cos(tAngle)
GetPt.Y = CenterY + Rad * Math.Sin(tAngle)
End Function
Excuse me. Gotta be somewhere else now. Will catch up with the explanation later.