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

前端 未结 9 1130
刺人心
刺人心 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: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};
    }
    

提交回复
热议问题