Jump to content
Developer Wiki and Function Reference Links ×

Find an angle between two points


Recommended Posts

Michael,

   Are you looking for the slope of the line that connects those points? If so, I think you want to use Vec2Ang(). It's a built-in VS call.

Var

   V :Vector;

...

V.x := p2X - p1X;   { your DeltaX }

V.y := p2Y - p1Y;   { your DeltaY }

V.z := 0;

Ang := Vec2Ang(V);

   You'll get angles in degrees in the range of +-180. If you want values from 0-360, then test for a negative angle and add 360 to it.

if (Ang<0) then Ang := Ang + 360;

   If you are looking for the angle between two vectors, the answer is a little more complicated, but easily done. Write back for more details.

HTH,

Raymond

  • Like 1
Link to comment

Michael,

Glad I could help. Vectors are easy once you get used to using them. As a data type, think of them as one variable name with three sub-parts: the X, Y, and Z components, or V.x, V.y, and V.z. Usually vectors contain data that is linked in XY or XYZ, but in a program they could contain 3 numbers that do not refer to spatial concepts, like Temperature, Hour, and Minute. You decide what goes in and what comes out. In this case the vector variable is just a convenient wrapper to keep three related values together under one name.

For spatial data here are some quick rules to help you think about vectors.

• You can store 2D or 3D points in a vector variable. If you store 2D data, just ignore the ".z" component. You can set it to 0, but it probably already is zero.

• You can store XY or XYZ displacements in a vector (deltaX, deltaY [, deltaZ]).

• If you add or subtract 2 vectors, you'll get a vector as the answer:

   V3 := V1 + V2;

   V2 := V3 - V1;   { same expression, just rearranged }

• if you add a vector and a point, the answer will be another point:

   P2 := P1 + Voffset;   { Voffset is a displacement vector showing where P2 is relative to P1 }

• Subtracting 1 point from another point returns a vector showing the displacement between the points:

   Voffset := P2 - P1;

• Adding 2 points is somewhat meaningless, unless you think of their positions as vector displacements from the origin.

Another thing to do that helps keep your programs clear is to save data directly to vector fields. Also start vector variable names with a "V" and point variables with a "P". It will help readability.

Try this as an example:

PROCEDURE xxx;
{ On a design layer with one Line selected, run script to show the angle and length of the Line. }
VAR
Ang, Lngth :Real;
P1, P2, V :Vector;
BEGIN
GetSegPt1(FSActLayer, P1.x, P1.y);	{ starting point of Line }
GetSegPt2(FSActLayer, P2.x, P2.y);	{ ending point of Line }

V := P2 - P1;	{ displacement between points P2 & P1 }

Ang := Vec2Ang(V);	{ angle of vector V }
Lngth := Norm(V);	{ length of vector V }
Message('Angle = ', Ang, '°    Length = ', Lngth);
SysBeep;
END;
Run(xxx);

Welcome to the best part of Vector-scripting. It's all in the name ;-)

Raymond

Link to comment
OK, that's cool.

Can I divide vectors by integers to get points 1/2, 1/3, 1/4 along a vector?

Mind blown.

mk

While you can probably divide a vector it won't give you the expected result.

Calculate the unit vector (direction) and the magnitude of the original vector.

Then any point along the vector = original point + direction * magnitude / divider.

Edited by Hippocode
Link to comment

YES! Vectors scale with multiplication and division by a constant.

Vmid := 0.5 * V;

or

Vmid := V / 2;

Same result.

It is the same as scaling each component individually:

Vmid.x := V.x / 2;

Vmid.y := V.y / 2;

Vmid.z := V.z / 2;

Big time saver when writing code, and it and makes reading it later a lot easier to follow.

There are tons of sites on the web about Vector operations. Google and learn. It is very visual so learning is easy. Anyone who can read a map and follow sequential directions already has the basics of Vector arithmetic mastered. Vector math is only a formal way of stating what you already can see spatially. It's an algebraic language, like Pascal, C, and Python are procedural languages, nothing more. Learn the grammar and never look back.

Raymond

Link to comment

Hippocode is correct, but his answer may be more complicated than what you need. It is more generic as it applies both to finding points between your endpoints, and also to finding points anywhere along the line passing through your two points.

Elaborating on what I posted earlier:

The vector V between points P1 and P2 is described as:

V := P2 - P1;

Its length Norm(V) is the distance between the two points.

Its angle Vec2Ang(V) points in the direction from P1 to P2.

V2 := V/2; is half as long as the distance from P1 to P2. The direction is the same but the length is half of V.

To get the midpoint between P1 and P2 you add V2 to P1.*

Similarly, let V3 := V/3;

So P1 + V3 is 1/3 the distance between the two points.

Similarly V4 := V/4, and P1 + V4 is one quarter the way between the points.

Etc.

You can add repetitively to get other multiples of points along the line:

P1 + V3 is 1/3 from P1 to P2

P1 + V3 + V3 is 2/3 from P1 to P2

P1 + V3 + V3 + V3 is 3/3 from P1 to P2, or at the same position as P2.

Elaborating on what Hippocode suggested:

What Hippocode is proposing is to make V1 := UnitVec(V).

That is, a vector of length 1 and the same direction as V.

If you know the length of V (hint: Norm(V)) then you can scale the "unit vector" by any fraction you want, larger or smaller.

NewVector := UnitV * OriginalLength * ScaleFactor;

Then any new point is P1 + NewVector.

Your original vector can be written this way with a scale factor of 1:

V := UnitV * OriginalLength * 1;

This is the more generic way of thinking about finding points along a line, but it may be more algebra than you need for a simple problem. What you will find is there are usually multiple ways to solve a problem.

HTH,

Raymond

* Another concise way to find the midpoint between two points is:

Pmid := (P1 + P2) / 2;

It evaluates to the same value as P1 + (P2 - P1) / 2;

This expression does not work for finding the 1/3 point, etc.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...