Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.62 KB | None | 0 0
  1. class Vector3{
  2.   constructor(x,y,z){
  3.     this.x = x;
  4.     this.y = y;
  5.     this.z = z;
  6.   }
  7. }
  8.  
  9. class Vector2{
  10.   constructor(x,y){
  11.     this.x = x;
  12.     this.y = y;
  13.   }
  14. }
  15.  
  16. function transformation(point, cam_pos, cam_angle, display){
  17.   x = point.x - cam_pos.x;
  18.   y = point.y - cam_pos.y;
  19.   z = point.z - cam_pos.z;
  20.   dx = Math.cos(cam_angle.y) * (Math.sin(cam_angle.z) * y + Math.cos(cam_angle.z) * x) - Math.sin(cam_angle.y) * z;
  21.   dy = Math.sin(cam_angle.x) * (Math.cos(cam_angle.y) * z + Math.sin(cam_angle.y) * (Math.sin(cam_angle.z) * y + Math.cos(cam_angle.z) * x)) + Math.cos(cam_angle.x) * (Math.cos(cam_angle.z) * y - Math.sin(cam_angle.z) * x);
  22.   dz = Math.cos(cam_angle.x) * (Math.cos(cam_angle.y) * z + Math.sin(cam_angle.y) * (Math.sin(cam_angle.z) * y + Math.cos(cam_angle.z) * x)) - Math.sin(cam_angle.x) * (Math.cos(cam_angle.z) * y - Math.sin(cam_angle.z) * x);
  23.  
  24.   bx = (display.z/dz) * dx + display.x;
  25.   by = (display.z/dz) * dy + display.y;
  26.   point2D = new Vector2(bx,by);
  27.   return point2D;
  28. }
  29.  
  30. function rotatex(point, angle){
  31.   xn = point.x;
  32.   yn = point.y * Math.cos(angle) - point.z * Math.sin(angle);
  33.   zn = point.y * Math.sin(angle) + point.z * Math.cos(angle);
  34.   return new Vector3(xn,yn,zn);
  35. }
  36.  
  37. function rotatey(point, angle){
  38.   xn = point.x * Math.cos(angle) - point.z * Math.sin(angle);
  39.   yn = point.y;
  40.   zn = point.z * Math.cos(angle) + point.x * Math.sin(angle);
  41.   return new Vector3(xn,yn,zn);
  42. }
  43.  
  44. function rotatez(point, angle){
  45.   xn = point.x * Math.cos(angle) - point.y * Math.sin(angle);
  46.   yn = point.x * Math.sin(angle) + point.y * Math.cos(angle);
  47.   zn = point.z;
  48.   return new Vector3(xn,yn,zn);
  49. }
  50.  
  51. function translateXYZ(point, x, y, z){
  52.   xn = point.x + x;
  53.   yn = point.y + y;
  54.   zn = point.z + z;
  55.   return new Vector3(xn,yn,zn);
  56. }
  57.  
  58. function translateObject(vertices, x, y, z){
  59.   for (i = 0; i < vertices.length; i++){
  60.     vertices[i] = translateXYZ(vertices[i], x, y, z);
  61.   }
  62.   return vertices;
  63. }
  64.  
  65. function rotateObjectX(vertices, angleX){
  66.   for (i = 0; i < vertices.length; i++){
  67.     vertices[i] = rotatex(vertices[i], angleX);
  68.   }
  69.   return vertices;
  70. }
  71.  
  72. function rotateObjectY(vertices, angleY){
  73.   for (i = 0; i < vertices.length; i++){
  74.     vertices[i] = rotatey(vertices[i], angleY);
  75.   }
  76.   return vertices;
  77. }
  78.  
  79. let angleX = 0;
  80. let angleY = 0.01;
  81. let angleZ = 0;
  82.  
  83. let a = 16;
  84. let vertices  = [new Vector3(a,a,a), new Vector3(a,-a,a), new Vector3(-a, -a, a), new Vector3(-a,a,a), new Vector3(a,a,-a), new Vector3(a,-a,-a), new Vector3(-a,-a,-a), new Vector3(-a,a,-a) ];
  85. let edges  = [new Vector2(1,2), new Vector2(2,3), new Vector2(3,4), new Vector2(4,1), new Vector2(5,6), new Vector2(6,7), new Vector2(7,8), new Vector2(8,5), new Vector2(1,5), new Vector2(2,6), new Vector2(3,7), new Vector2(4,8)];
  86.  
  87. //let axes = [new Vector3(0,0,0), new Vector3(-600,0,0), new Vector3(0,-600,0), new Vector3(0,0,-600)];
  88. //let axes_conn = [new Vector2(1,2), new Vector2(1,3), new Vector2(1,4)];
  89.  
  90. let cam_angle = new Vector3(0,0,0);
  91. let display   = new Vector3(128, 128, 256);
  92. var cam_pos   = new Vector3(0, 0, 64);
  93.  
  94. function setup(){
  95.   createCanvas(1100,750);
  96.   frameRate(60);
  97.  
  98. }
  99.  
  100.  
  101. function mouseDragged(){
  102.   vertices = rotateObjectX(vertices, (mouseY - pmouseY)*0.005);
  103.   vertices = rotateObjectY(vertices, (mouseX - pmouseX)*0.005);
  104. }
  105.  
  106. let key  = false;
  107. let xdir = 0;
  108. let ydir = 0;
  109.  
  110. function keyPressed(){
  111.   if (keyCode === ENTER){
  112.     key = !key;
  113.   }
  114.   if (keyCode === 38){
  115.     ydir += 1;
  116.   }
  117.   if (keyCode === 40){
  118.     ydir -= 1;
  119.   }
  120.   if (keyCode === 37){
  121.     xdir += 1;
  122.   }
  123.   if (keyCode === 39){
  124.     xdir -= 1;
  125.   }
  126.   if (keyCode === 8){
  127.     ydir = 0;
  128.     xdir = 0;
  129.   }
  130. }
  131.  
  132. function draw(){
  133.   if (key){
  134.     background(0);
  135.     translateObject(vertices, xdir, ydir, 0);
  136.     for (i = 0; i < edges.length; i++){
  137.         var cur_edge = edges[i];
  138.         var pixel1 = transformation(vertices[cur_edge.x - 1], cam_pos, cam_angle, display);
  139.         var pixel2 = transformation(vertices[cur_edge.y - 1], cam_pos, cam_angle, display);
  140.         stroke('green');
  141.         line(pixel1.x | 0, pixel1.y | 0, pixel2.x | 0, pixel2.y | 0);
  142.         console.log(pixel1.x | 0, pixel1.y | 0, pixel2.x | 0, pixel2.y | 0);
  143.     }
  144.     /*
  145.     for (i = 0; i < axes_conn.length; i++){
  146.         var cur_edge = axes_conn[i];
  147.         var pixel1 = transformation(axes[cur_edge.x - 1], cam_pos, cam_angle, display);
  148.         var pixel2 = transformation(axes[cur_edge.y - 1], cam_pos, cam_angle, display);
  149.         stroke('green');
  150.         line(pixel1.x | 0, pixel1.y | 0, pixel2.x | 0, pixel2.y | 0);
  151.         console.log(pixel1.x | 0, pixel1.y | 0, pixel2.x | 0, pixel2.y | 0);
  152.     }
  153.     */
  154.   }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement