function setup() {
createCanvas(400, 400);
}
var count = 0;
function draw() {
background(250);
rectMode(CENTER);
stroke(0,0,255);
fill(0,0,255);
count += 1;
var box1X = 100;
var box1Y = 100;
var box2X = 160;
var box2Y = 100;
var box1R = count;
var box2R = -60-count;
var box1W = 50;
var box1H = 50;
var box2W = 50;
var box2H = 50;
translate(box1X, box1Y);
rotate(radians(box1R));
rect(0, 0, box1W, box1H);
rotate(radians(-box1R));
translate(-box1X, -box1Y);
translate(box2X, box2Y);
rotate(radians(box2R));
rect(0, 0, box2W, box2H);
rotate(radians(-box2R));
translate(-box2X, -box2Y);
stroke(255,0,0);
fill(255,0,0);
var pointRotated = [];
pointRotated.push(GetPointRotated(box1X, box1Y, box1R, -box1W/2, box1H/2)); // Dot1
pointRotated.push(GetPointRotated(box1X, box1Y, box1R, box1W/2, box1H/2)); // Dot2
pointRotated.push(GetPointRotated(box1X, box1Y, box1R, -box1W/2, -box1H/2)); // Dot3
pointRotated.push(GetPointRotated(box1X, box1Y, box1R, box1W/2, -box1H/2)); // Dot4
pointRotated.push(createVector(box1X, box1Y)); // Dot5
for (var i=0;i<pointRotated.length;i++){
ellipse(pointRotated[i].x,pointRotated[i].y,3,3);
}
}
function GetPointRotated(X, Y, R, Xos, Yos){
// Xos, Yos // the coordinates of your center point of rect
// R // the angle you wish to rotate
//The rotated position of this corner in world coordinates
var rotatedX = X + (Xos * cos(radians(R))) - (Yos * sin(radians(R)))
var rotatedY = Y + (Xos * sin(radians(R))) + (Yos * cos(radians(R)))
return createVector(rotatedX, rotatedY)
}
<script src="//cdnjs.cloudflare.com/ajax/libs/p5.js/0.3.3/p5.min.js"></script>
Mungkin, ada beberapa optimasi yang tersedia dengan membagi masalah menjadi dua:
Kode di bawah ini, di sini persegi panjang disebut penggaris. ruler.x, ruler, y adalah pusat persegi panjang.
sumber
Sedikit terlambat, tapi inilah fungsi ringkas yang saya gunakan. Ini menghitung poin atas dan kiri, lalu membalikkannya ke sudut yang berlawanan.
sumber
Posting lama, tapi di sini ada cara lain untuk melakukannya:
Semoga ini bisa membantu!
sumber