alexbol99 / flatten-js

Javascript library for 2d geometry
MIT License
535 stars 56 forks source link

HELP - What is the best way to create parallel segments ? #154

Closed rjsworking closed 10 months ago

rjsworking commented 10 months ago

Hi I've been trying to create parallel segments using translate() without success.

Example: I have a segment S1 segment(200, 200, 300, 600) and I want a 2nd segment S2 at a distance of 50px

Screenshot 2023-10-01 121630

The coords of new segment would be (248.5071250072666, 187.87321874818335, 348.5071250072666, 587.8732187481834)

Appreciate any help.

Cheers rjs

alexbol99 commented 10 months ago

Hi, @rjsworking ,

The library doesn't have built-in method to create parallel segments. You should create normal vector to your segment and then translate s1 on this vector multiplied by dist = 50:

const norm = vector(s1.start, s1.end).rotate90CW().normalize();
const s2 = s1.translate(norm.multiply(50));

Then you will get expected result:

ps: Point {x: 248.5071250072666, y: 187.87321874818335}
pe: Point {x: 348.5071250072666, y: 587.8732187481834}

Ask if you have more questions

rjsworking commented 10 months ago

Hi Alex,

Thank you very much!

Cheers rjs

alexbol99 commented 10 months ago

You are welcome