Ich habe diesen Code erstelltRotating-Platz in JavaScript (mit p5 & triganometry)
var points = [
[100, 100],
[100, -100],
[-100, -100],
[-100, 100]
];
function setup() {
createCanvas(400, 400);
}
function draw() {
background(255);
translate(200, 200);
fill(0);
beginShape();
for (var x = 0; x < points.length; x++) {
points[x] = rotatePoint(0, 0, points[x][0], points[x][1], 1);
vertex(points[x][0], points[x][1]);
console.log("vertex: "+String(points[x][0])+" "+String(points[x][1]));
}
endShape(CLOSE);
}
function getVectDist(p1X, p1Y, p2X, p2Y) {
var deltaX = p1X-p2X;
var deltaY= p1Y-p2Y;
var vect=[deltaX, deltaY];
return vect;
}
//Function to rotate a point around the origin or first point
function rotatePoint(originX, originY, pointX, pointY, angle) {
var dist = getVectDist(originX, originY, pointX, pointY);
//calculating the hypotenuse of dist
var hyp = Math.sqrt((dist[0]*dist[0])+(dist[1]*dist[1]));
console.log("hypotenuse"+String(hyp));
//calculating coords
var y = Math.cos(angle) * hyp;
var x = Math.sin(angle) * hyp;
return [x, y];
}
zu versuchen und einen rotierenden Platz zu schaffen. Es erzeugt jedoch nur eine seltsame, Projektil-artige, sich bewegende diagonale Linie, die sich um 45 Grad bewegt, bevor sie sich in ein einzelnes Pixel in der Mitte des Bildschirms verwandelt. Weiß jemand warum? danke.
Können Sie bitte einen Link zu einem CodePen oder einem JSFiddle, auf dem Ihr Code läuft? –