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

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

      function getFirstPoint(x1,y1,x2,y2,l1,l2)
        distanceV = {x2 - x1, y2 - y1}
        vlen = math.sqrt(distanceV[1]^2 + distanceV[2]^2)
        normalized = {distanceV[1] / vlen, distanceV[2] / vlen}
        rotated = {-normalized[2], normalized[1]}
        p1 = {x1 - rotated[1] * l1 / 2, y1 - rotated[2] * l1 / 2}
        p2 = {p1[1] + rotated[1] * l1, p1[2] + rotated[2] * l1}
        p3 = {p1[1] + normalized[1] * l2, p1[2] + normalized[2] * l2}
        p4 = {p3[1] + rotated[1] * l1, p3[2] + rotated[2] * l1}
        points = { p1 , p2 , p3 , p4}
        return p1
    end
    

提交回复
热议问题