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

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

    It's definitely a rectangle? Then you know the orientation of the short sides (they're parallel to the line between your points), and hence the orientation of the long sides.

    You know the orientation and length of the long sides, and you know their midpoints, so it's straightforward to find the corners from there.

    Implementation is left as an exercise to the reader.

    0 讨论(0)
  • 2021-01-05 02:21
    /* rcx = center x rectangle, rcy = center y rectangle, rw = width rectangle, rh = height rectangle, rr = rotation in radian from the rectangle (around it's center point) */
    
    function toRectObjectFromCenter(rcx, rcy, rw, rh, rr){
        var a = {
            x: rcx+(Math.sin((rr-degToRad(90))+Math.asin(rh/(Math.sqrt(rh*rh+rw*rw)))) * (Math.sqrt(rh*rh+rw*rw)/2)), 
            y: rcy-(Math.cos((rr-degToRad(90))+Math.asin(rh/(Math.sqrt(rh*rh+rw*rw)))) * (Math.sqrt(rh*rh+rw*rw)/2))
        };
        var b = {
            x: a.x+Math.cos(rr)*rw,
            y: a.y+Math.sin(rr)*rw
        };
        var c = {
            x: b.x+Math.cos(degToRad(radToDeg(rr)+90))*rh,
            y: b.y+Math.sin(degToRad(radToDeg(rr)+90))*rh
        };
        var d = {
            x: a.x+Math.cos(degToRad(radToDeg(rr)+90))*rh,
            y: a.y+Math.sin(degToRad(radToDeg(rr)+90))*rh
        };
        return {a:a,b:b,c:c,d:d};
    }
    
    0 讨论(0)
  • 2021-01-05 02:22

    This means that there will be two lines parallel to the line between the two points you have. Get the corners by translating the line you have 1/2 the length of the top side in each direction perpendicular to the line you have.

    0 讨论(0)
提交回复
热议问题