Skip to main content
GameDev.net gamedev.net
🔒 Locked

Vector

Started by garusher Nov 15, 2011 at 3:33 PM 0 replies 800+ views
Original Post
garusher
garusher
Hi

I am wanting to create a rectangle for two points (vector). I need a function that takes two points and a length and calculates a perpendicular point from line (vector) AB, to a given length.

I found this code, however it doesn't seem to work.


Private Function doit(ByVal a As PointF, ByVal b As PointF, ByVal l As Double) As PointF Dim lfraction, dx, dy As Double dx = b.X - a.X dy = b.Y - a.Y If dx = 0 And dy = 0 Then Exit Function lfraction = l / Math.Sqrt(dx * dx + dy * dy) Return New Point(b.X + lfraction * dy, b.Y + lfraction * dx) End Function
Is there any easy or other methods i can use to create a rectangle from a line

Maybe a line stroke method or something.

Thanks

Gary

alvaro
alvaro
I am not sure I understand what you are saying, so let me know if this doesn't answer your question.

If you have a vector (x,y), the vectors (y,-x) and (-y,x) are perpendicular to it (you have a choice as to whether you want to rotate 90 degrees one way or the other). If you want a vector of a particular length, you can divide by the length of the vector and multiply by the desired length.

If you now have points (Ax,Ay) and (Bx,By), you can compute a point (Cx,Cy) such that the vectors (Bx-Ax,By-Ay) and (Cx-Ax,Cy-Ay) are perpendicular and (Cx-Ax,Cy-Ay) has the desired length:
Vx = By-Ay
Vy = -(Bx-Ax)
initial_length = sqrt(Vx*Vx+Vy*Vy)
scale = desired_length/initial_length
scaled_Vx = Vx*scale
scaled_Vy = Vy*scale
Cx = Ax + scaled_Vx
Cy = By + scaled_Vy

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.