Jump to content
Developer Wiki and Function Reference Links ×

changing Line length


Recommended Posts

Embarrassing question #382

 

In my script, I've drawn a line, and rotated it.  Now I want to change the length of the line, while keeping it's first point (Pt1) stationary.  I feel like there should be a simple command like SetLLength(); but I can't find anything like that, or do I just need to recalculate Pt2 using a bunch of trig?  Hoping there's an easy way I don't know about.

 

Dave 

Link to comment

Hi @Pat Stanford, @DCarpenter,

   Here's an example using vectors, as requested, and a little more,  😉,   though HScale2D() may be quicker for lines. Where this method may be more useful is when you are working with a Polygon and want to move a vertex without changing anything else. As with anything, "Actual mileage may vary."

 

 

   Scale a line using a vector approach:

PROCEDURE xxx;
{ Test script to set new line length using vectors. }
{ 22 April 2024 - Raymond Mullin }
VAR
	H :Handle;
	NewLength :Real;


	procedure SetLineLength(H :Handle; NewLength :Real);
	{ Sets length of line H to new value NewLength. Point P1 is the anchor point.}
	Var
		P1, P2 :Vector;
	Begin
		GetSegPt1(H, P1.x, P1.y);
		GetSegPt2(H, P2.x, P2.y);
		P2 := P1 + UnitVec(P2-P1) * NewLength;
		SetSegPt2(H, P2.x, P2.y);	
	End;		{ SetLineLength }
	
	
BEGIN
	H := FSActLayer;
	NewLength := RealDialog('How long?', concat(hLength(H)));
	if not DidCancel then 
		SetLineLength(H, NewLength);	{ changes line length to new value }
	SysBeep;	{ make noise when done }
END;
Run(xxx);

 

   If you want to scale the line in the other direction, swap the vectors P1 & P2 as shown in the following line.   

        P1 := P2 + UnitVec(P1-P2) * NewLength;

 

   Here's an explanation of the expression:  P2 := P1 + UnitVec(P2-P1) * NewLength;

 

1) Where P1 and P2 are both vectors, (P2-P1) is the vector that points from point P1 to point P2. Likewise, (P1-P2) is the opposite vector that points from point P2 to point P1.

2) UnitVec() is a function that scales a vector to have a length of "1".

3) A constant times a vector scales the vector, so scaling a unit vector by the new length results in a vector of the desired length.

       Essentially, the length is changed, but the direction remains the same.

4) Since vectors are positionless, adding a vector to a known point will return the point that is the correct distance and the correct direction away from the known point.

 

PS - If you are wondering why I used Vector instead of Point as the variable's data type, The UnitVec() function does not accept Point type data. Essentially I am only working with 2D data, and Point and Vector are similar, but Vector data types work in all of the Vectorworks expressions, so I always use the Vector data type. In this case the z-component of the vector is always "0" and therefor does not contribute to any of the calculations. Using Vectors for 2D calculations saves having to do data conversions in the middle of a script between Points and Vectors. Feel free to try other ways as mine is not the only one.

 

Raymond

 

 

 

 

  • Like 2
Link to comment

@MullinRJ - that's amazing.  I think vectors are very powerful but I have a hard time getting my brain wrapped around them.  I think because they looks so similar (Pt.x, Pt.y, and Vec.x,Vec.y) I have a hard time understanding how I use them differently.  Is there a good place to help me understand vectors better?

 

Dave

Link to comment

Hi Dave,

   Essentially vectors are a Structure of 3 numbers, or an Array of 3 numbers, under one variable name. There's lots of material online, but if you give me a call I can get you started quite quickly, especially in ways to use them in VW. PM me and we can setup a Skype, Zoom, or "other" conference call at your convenience. 

 

Seriously, call me,

Raymond

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