Knowing two points of a rectangle, how can I figure out the other two?

前端 未结 9 1183
刺人心
刺人心 2021-01-05 01:47

Hey there guys, I\'m learning processing.js, and I\'ve come across a mathematical problem, which I can\'t seem to solve with my limited geometry and trigonometry knowledge o

9条回答
  •  粉色の甜心
    2021-01-05 02:18

    Calculate the angle of the line joining the two midpoints using an arc-tangent function applied to the vector you get between them.

    Subtract 90 degrees from that angle to get the direction of the top edge

    Starting from the top-center point, move relative (1/2 top width x sin(angle), 1/2 top width x cos(angle)) - that gets the top right corner point.

    Continue around the rectangle using the sin and cos of the angles and widths as appropriate

    As a test: Check you made it back to the starting point

提交回复
热议问题