Stanko / offset-polygon

Small, no dependency library for offsetting polygons.
https://muffinman.io/offset-polygon/
MIT License
35 stars 2 forks source link

Question: What does `inwardEdgeNormal` do? #5

Closed bambinoua closed 5 months ago

bambinoua commented 5 months ago

What does inwardEdgeNormal do? What does it return?

Stanko commented 5 months ago

Hey, I'll start with a disclaimer, it was a while, and most of this code isn't originally written by me. But inwardEdgeNormal, which is defined here: https://github.com/Stanko/offset-polygon/blob/main/src/offset-polygon.ts#L35C26-L45C2 returns a normalized vector that is normal to the edge defined by it's arguments vertex1 and vertex1. The vector is pointed inward to the polygon the vertices are part of (with the assumption they are in clockwise order).

You'll notice there is also outwardEdgeNormal method which just reverses the direction of inwardEdgeNormal vector.

I hope this makes sense.

Cheers!

bambinoua commented 5 months ago

@Stanko thank you for a quick response. I need to implement inflate/deflate functionality for polygon using another programming language but I am a real newbie in vector mathematics :) so I am in search of any resources where I can read something to understand.

normalized vector that is normal to the edge defined by it's arguments vertex1 and vertex1

What is a normalized vector? The vector which is a perpendicular to surface where vertex1 and vertex2 are laying?

The vector is pointed inward to the polygon the vertices are part of (with the assumption they are in clockwise order).

Why is it so? Why the specific structure is returned, namely x is negative but y is positive? How to decide what order should be clocwise or counter-clockwise? Could you please to point me some resources where I can read about such things? (if you know of course). Thanks.

Stanko commented 5 months ago

Normalized vector is just a vector of length 1. Also called a unit vector sometimes. Simple example - if you have a vector x: 10, y: 0, it's normalized version would be x: 1, y: 0.

Using clockwise order for polygon is just a convention that is common in computer graphics.

To calculate these normals, you need to swap x and y and to swap the sign in front of the one of them. So if we have a vector v, to get two vectors to normal to it:

They are pointing to the opposite sides.

In our case inwardEdgeNormal uses the n1 version which points inwards.

Again, I hope this makes sense, I learned math originally in my language and I'm not sure how to explain certain things in English :) As for resources, I don't have anything specific, I usually just search for a problem I have.

Stanko commented 5 months ago

P.S. For me, it really helps if I draw these things on paper. Try drawing these normal vectors (by swapping x/y and a sign) and I think it will be easier to undersand.

bambinoua commented 5 months ago

@Stanko thank you again. My brain becomes clean a little. :)

Using clockwise order for polygon is just a convention that is common in computer graphics.

Where and for what this order is used?

To calculate these normals, you need to swap x and y and to swap the sign in front of the one of them.

Spent time yesterday but could not find info about swap coordinates or signs. Everywhere I read about normals to surfaces but not to lines. :( Will continue.

For me, it really helps if I draw these things on paper.

Hm.. how can I draw a vector? I need to have 2 points with coordinates. Or do you mean to start them from origin?

P.S. I've found this link https://paulbourke.net/geometry/pointlineplane/ in your code. And I didn't understand what does the u in equation P = P1 + u (P2 - P1) mean?

Thanks a lot for your explanations.

Stanko commented 5 months ago

Orientation (clockwise / counter-clockwise) is used in multiple places. Like you noticed, there are two normal vectors for each vector. Orientation provides a way to differentiate between the two. Other example is that in most graphic programs, CW orientation is used for polygons and CCW for "holes" to differentiate between the two:

326701799-b4de7219-673f-421f-a600-d9eba109a7e7

As for drawing vectors, you are right, you are drawing them from the origin. Here is an example with two normals drawn in green:

Screenshot 2024-04-30 at 08 54 23

I'm pretty sure u stands for "unit vector" aka normalized vector - the vector of length 1.

You are welcome! I don't mind answering these, but you are probably benefit more if you find a book or a course that covers introduction to vectors. Maybe Dan Shiffman's Nature of Code:

Cheers!

Stanko commented 4 months ago

@bambinoua I just stumbled on this website which has interactive lessons on vectors and linear algebra in general, I hope it helps! http://immersivemath.com/ila/index.html