Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Point{
  2.     constructor(x = 0, y = 0){
  3.         this.x = x;
  4.         this.y = y;
  5.     }
  6. };
  7.  
  8. function rotate(pointTo, center, angleInDeg){
  9.     var newX = (pointTo.x - center.x)*Math.cos(angleInDeg/180*Math.PI)-(pointTo.y - center.y)*Math.sin(angleInDeg/180*Math.PI) + center.x;
  10.     var newY = (pointTo.x - center.x)*Math.sin(angleInDeg/180*Math.PI)+(pointTo.y - center.y)*Math.cos(angleInDeg/180*Math.PI) + center.y;
  11.     var newPoint = new Point(newX, newY);
  12.     return newPoint;
  13. }
  14.  
  15. function genPolygon(center, radius, n){
  16.     var polygon = [];
  17.     polygon[0] = new Point(center.x, center.y-radius);
  18.     for(var i = 1; i < n; i ++){
  19.         polygon[i] = rotate(polygon[i-1], center, 360/n);
  20.     }
  21.     return polygon;
  22. }
  23.  
  24. function drawPolygon(polygon){
  25.     context.beginPath();
  26.     context.lineWidth = 10;
  27.     context.strokeStyle = "#e21b1b";
  28.     context.moveTo(polygon[0].x, polygon[0].y);
  29.     for(var i = 1; i < polygon.length; i ++){
  30.         context.lineTo(polygon[i].x, polygon[i].y);
  31.     }
  32.     context.closePath();
  33.     context.stroke();
  34.    
  35. }
  36. // Creating variables
  37. var myX = 0, myY = 0;
  38.  
  39. function update() {
  40.     myX = myX+(mouseX-myX)/10;
  41.     myY = myY+(mouseY-myY)/10;
  42. }
  43.  
  44. function draw() {
  45.     drawPolygon(genPolygon(new Point(100, 100), 40, 3));
  46. };
  47.  
  48. function keyup(key) {
  49.     // Show the pressed keycode in the console
  50.     console.log("Pressed", key);
  51. };
  52.  
  53. function mouseup() {
  54.     // Show coordinates of mouse on click
  55.     console.log("Mouse clicked at", mouseX, mouseY);
  56. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement