Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Point{
- constructor(x = 0, y = 0){
- this.x = x;
- this.y = y;
- }
- };
- function rotate(pointTo, center, angleInDeg){
- var newX = (pointTo.x - center.x)*Math.cos(angleInDeg/180*Math.PI)-(pointTo.y - center.y)*Math.sin(angleInDeg/180*Math.PI) + center.x;
- var newY = (pointTo.x - center.x)*Math.sin(angleInDeg/180*Math.PI)+(pointTo.y - center.y)*Math.cos(angleInDeg/180*Math.PI) + center.y;
- var newPoint = new Point(newX, newY);
- return newPoint;
- }
- function genPolygon(center, radius, n){
- var polygon = [];
- polygon[0] = new Point(center.x, center.y-radius);
- for(var i = 1; i < n; i ++){
- polygon[i] = rotate(polygon[i-1], center, 360/n);
- }
- return polygon;
- }
- function drawPolygon(polygon){
- context.beginPath();
- context.lineWidth = 10;
- context.strokeStyle = "#e21b1b";
- context.moveTo(polygon[0].x, polygon[0].y);
- for(var i = 1; i < polygon.length; i ++){
- context.lineTo(polygon[i].x, polygon[i].y);
- }
- context.closePath();
- context.stroke();
- }
- // Creating variables
- var myX = 0, myY = 0;
- function update() {
- myX = myX+(mouseX-myX)/10;
- myY = myY+(mouseY-myY)/10;
- }
- function draw() {
- drawPolygon(genPolygon(new Point(100, 100), 40, 3));
- };
- function keyup(key) {
- // Show the pressed keycode in the console
- console.log("Pressed", key);
- };
- function mouseup() {
- // Show coordinates of mouse on click
- console.log("Mouse clicked at", mouseX, mouseY);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement