DCarpenter Posted April 22 Share Posted April 22 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 Quote Link to comment
Pat Stanford Posted April 22 Share Posted April 22 HScale2D setting the center of the scale at the point you want to keep fixed and then just set the scale factor per the difference between the current length and new length. @MullinRJWill probably chime in here soon with an easier way to do this using vectors. 😉 Quote Link to comment
DCarpenter Posted April 22 Author Share Posted April 22 I like that logic of thought. HScale2D, sounds like what I was looking for. Thank you, Dave Quote Link to comment
MullinRJ Posted April 23 Share Posted April 23 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 2 Quote Link to comment
Pat Stanford Posted April 23 Share Posted April 23 @MullinRJ Thanks for the help. I THINK I could have done this, but your explanation is so much clearer than mine would have been. And at the time, my brain would not tell me how to make the original vectors/points. Thanks for the refresher. 1 Quote Link to comment
DCarpenter Posted April 23 Author Share Posted April 23 @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 Quote Link to comment
MullinRJ Posted April 23 Share Posted April 23 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 Quote Link to comment
DCarpenter Posted April 25 Author Share Posted April 25 You are the best, thank you. I'll try and PM you in a day or two. Thank you, Dave Quote Link to comment
Recommended Posts
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.