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

前端 未结 9 1127
刺人心
刺人心 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:12

    If this quadrilateral is a rectangle (all four angles are 90 degrees), then it can be solved. (if it could be any quadrilateral, then it is not solvable)

    if the points are (x1,y1), and (x2, y2), and if the two points are not perfectly vertical (x1 = x2) or horizontal (y1 = y2), then the slope of one edge of the rectangle is

    m1 = (y2-y1) / (x2-x1) 
    

    and the slope of the other edge is:

    m2 = - 1 / m1
    

    If you know the lengths of the sides, and the midpoints of two opposite sides, then the corrner points are easily determined by adding dx, dy to the midpoints: (if L is length of the sides that the midpoints are on)

    dx = Sqrt( L^2 / (1 + m2^2) ) / 2
    

    and

    dy = m2 * dx
    

    NOTE: if the points are vertically or horizontally aligned, this technique will not work, although the obvious solution for those degenerative cases is much simpler.

提交回复
热议问题