alexbol99 / flatten-js

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

Segment Wrong points after rotate? #164

Closed SpaceNinjaApe closed 4 months ago

SpaceNinjaApe commented 4 months ago

Following code

let l1 = new Segment(
    new Point(-200,0),
    new Point(200,0)
)
console.log(`middle: ${l1.middle().x}, ${l1.middle().y}`)
console.log(`start : ${l1.start.x}, ${l1.start.y}`)
console.log(`length: ${l1.length}`)
console.log()
console.log("rotate(90)")
l1 = l1.rotate(90)
console.log()
console.log(`middle: ${l1.middle().x}, ${l1.middle().y}`)
console.log(`start : ${l1.start.x}, ${l1.start.y}`)
console.log(`length: ${l1.length}`)

Gives me following output:

middle: 0, 0
start : -200, 0
length: 400

rotate(90)

middle: 0, 0
start : 89.61472322583404, -178.79933272011158
length: 400

I would expect:

middle: 0, 0
start : -200, 0
length: 400

rotate(90)

middle: 0, 0
start : 0, 200
length: 400

Am i missing something? Even with specifying the center for Rotation, it still gives me unexpected outputs.

alexbol99 commented 4 months ago

Hi, @SpaceNinjaApe , Sorry, but you are missing two points: 1) Angle is in radians 2) Y-axis oriented upwards So, if you rotate segment by angle Math.PI/2 (positive angle rotates counter clockwise), the result will be something like:

l1 = l1.rotate(Math.PI/2)

start : -1.2246467991473532e-14, -200

Best, Alex

SpaceNinjaApe commented 4 months ago

Thanks for the Reply @alexbol99 , okay i didnt now about the radians

But shouldnt l1.rotate(Math.PI) result in the exact same oposite Position ?

Theres always a slight difference to what i expect. Probably due to the nature of PI? How to deal with this? Round / Floor?

alexbol99 commented 4 months ago

Float calculations always end with small inaccuracy. You should use rounding

SpaceNinjaApe commented 4 months ago

Alright Thanks! keep up the good work lad!