# Vector

This topic is 2890 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 FunctionIs 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

##### Share on other sites
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

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 13
• 30
• 9
• 16
• 12