Advertisement
Guest User

Problematic code

a guest
Sep 2nd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.80 KB | None | 0 0
  1. class Vector {
  2. float[] pos;
  3.  
  4. Vector (float[] pos_) {
  5. pos=pos_;
  6. }
  7. Vector () {}
  8. void changeVector (float[] pos_) {
  9. pos=pos_;
  10. }
  11. void addFloat (float pos_) {
  12. if (pos==null) {
  13. System.err.println("(!) Position of vector is null!");
  14. } else {
  15. pos=expand(pos,pos.length+1);
  16. pos[pos.length-1]=pos_;
  17. }
  18. }
  19. void logSelf() {
  20. for (int i=0;i<pos.length;i++) {
  21. println(pos[i] + " ");
  22. }
  23. println("-----------------------");
  24. }
  25. void logErrorSelf() {
  26. for (int i=0;i<pos.length;i++) {
  27. System.err.println(pos[i] + " ");
  28. }
  29. System.err.println("-----------------------");
  30. }
  31. }
  32.  
  33. class Vectorlist {
  34. Vector[] v;
  35.  
  36. Vectorlist (Vector[] v_) {
  37. v=v_;
  38. }
  39.  
  40. Vectorlist () {}
  41. void changeVectorlist (Vector[] v_) {
  42. v=v_;
  43. }
  44.  
  45. void addVector (Vector v_) {
  46. Vector[] temp;
  47. if (v==null) {
  48. System.err.println("(!) Vectorlist is null!");
  49. } else {
  50. temp = new Vector[v.length+1];
  51. for (int i=0;i<v.length+1;i++) {
  52. temp[i]=v[i];
  53. }
  54. temp[temp.length-1]=v_;
  55. v = new Vector[v.length+1];
  56. v = temp;
  57. }
  58. }
  59.  
  60. Vector getVector (int v_) {
  61. return v[v_];
  62. }
  63.  
  64. float[][] getFloats() {
  65. float[][] temp = new float[v.length][v[0].pos.length];
  66. for (int i=0;i<v.length+1;i++) {
  67. for (int j=0;j<v[0].pos.length+1;j++) {
  68. temp[i][j]=v[i].pos[j];
  69. }
  70. }
  71. return temp;
  72. }
  73. }
  74.  
  75. class Matrix {
  76. float[][] matrix;
  77.  
  78. Matrix (float[][] matrix_) {
  79. matrix=matrix_;
  80. }
  81.  
  82. Matrix (Vectorlist v_) {
  83. if (v_==null) {
  84. System.err.println("(!) Vectorlist is null!");
  85. }else{
  86. matrix=v_.getFloats();
  87. }
  88. }
  89.  
  90. Matrix () {}
  91.  
  92. Vector multiply (Vector v) {
  93. if (v.pos.length!=matrix[0].length) {
  94. System.err.println("(!) Matrix incompatible with vector.");
  95. System.err.println("");
  96. System.err.println("Matrix:");
  97. System.err.println("Size: "+matrix[0].length+"x"+matrix.length);
  98. logErrorSelf();
  99. System.err.println("");
  100. System.err.println("Vector:");
  101. System.err.println("Size: "+v.pos.length);
  102. v.logErrorSelf();
  103. exit();
  104. return null;
  105. }
  106. float[] fo = new float[0];
  107. Vector vo = new Vector();
  108. for (int i=0;i<matrix.length;i++) {
  109. float sum = 0;
  110. for (int j=0;j<v.pos.length;j++) {
  111. sum += v.pos[j]*matrix[i][j];
  112. }
  113. fo = expand(fo,fo.length+1);
  114. fo[fo.length-1] = sum;
  115. }
  116. vo.changeVector(fo);
  117. return vo;
  118. }
  119.  
  120. Vectorlist multiply (Vectorlist v_) {
  121. Vectorlist temp = new Vectorlist();
  122. if (v_.v==null) {
  123. System.err.println("(!) Vectorlist is null!");
  124. return null;
  125. }else{
  126. for (int i=0;i<v_.v.length;i++) {
  127. temp.addVector(multiply(v_.v[i]));
  128. }
  129. return temp;
  130. }
  131. }
  132.  
  133. void logSelf() {
  134. for (int i=0;i<matrix.length;i++) {
  135. for (int j=0;j<matrix[0].length;j++) {
  136. print(matrix[i][j] + " ");
  137. }
  138. println();
  139. }
  140. println("-----------------------");
  141. }
  142.  
  143. void logErrorSelf() {
  144. for (int i=0;i<matrix.length;i++) {
  145. for (int j=0;j<matrix[0].length;j++) {
  146. System.err.print(matrix[i][j] + " ");
  147. }
  148. System.err.println();
  149. }
  150. System.err.println("-----------------------");
  151. }
  152. }
  153.  
  154. Matrix combineMatricies(Matrix a,Matrix b) {
  155. if (((a==null) || (b==null)) || ((a.matrix==null) || (b.matrix==null))) {
  156. System.err.println("(!) Matricies are null!");
  157. exit();
  158. return null;
  159. }
  160. if (a.matrix.length!=b.matrix[0].length) {
  161. System.err.println("(!) Matricies are incopmpatible.");
  162. System.err.println("");
  163. System.err.println("Matrix A:");
  164. System.err.println("Size: "+a.matrix[0].length+"x"+a.matrix.length);
  165. a.logErrorSelf();
  166. System.err.println("");
  167. System.err.println("Matrix B:");
  168. System.err.println("Size: "+b.matrix[0].length+"x"+b.matrix.length);
  169. b.logErrorSelf();
  170. exit();
  171. return null;
  172. }
  173. Vector vtemp = new Vector();
  174. Vectorlist ltemp = new Vectorlist();
  175. Matrix c;
  176. for (int j=0;j<a.matrix[0].length;j++) {
  177. for (int i=0;i<a.matrix.length;i++) {
  178. vtemp.addFloat(a.matrix[i][j]);
  179. }
  180. ltemp.addVector(vtemp);
  181. }
  182. c = new Matrix(b.multiply(ltemp));
  183. return c;
  184. }
  185.  
  186. Matrix createTranslation(float x, float y, float z, Vectorlist v_) {
  187. float[][] temp = {
  188. {1,0,0,x},
  189. {0,1,0,y},
  190. {0,0,1,z}
  191. };
  192. for (int i=0;i<v_.v.length;i++) {
  193. v_.v[i].addFloat(1);
  194. }
  195. return new Matrix(temp);
  196. }
  197.  
  198. Matrix createTranslation(float x, float y, float z, Vector v_) {
  199. float[][] temp = {
  200. {1,0,0,x},
  201. {0,1,0,y},
  202. {0,0,1,z}
  203. };
  204. v_.addFloat(1);
  205. return new Matrix(temp);
  206. }
  207.  
  208. Matrix createRotation(float xt, float yt, float zt) {
  209. float xc=cos(xt),xs=sin(xt);
  210. float yc=cos(yt),ys=sin(yt);
  211. float zc=cos(zt),zs=sin(zt);
  212. float[][] x = {
  213. {1,0,0},
  214. {0,xc,-xs},
  215. {0,xs,xc}
  216. };
  217. float[][] y = {
  218. {yc,0,ys},
  219. {0,1,0},
  220. {-ys,0,yc}
  221. };
  222. float[][] z = {
  223. {zc,-zs,0},
  224. {zs,zc,0},
  225. {0,0,1}
  226. };
  227. Matrix xRot = new Matrix(x);
  228. Matrix yRot = new Matrix(y);
  229. Matrix zRot = new Matrix(z);
  230. Matrix Rot = combineMatricies(combineMatricies(zRot,yRot),xRot);
  231. return Rot;
  232. }
  233.  
  234. Matrix createScale(float x, float y, float z) {
  235. float[][] m = {
  236. {x,0,0},
  237. {0,y,0},
  238. {0,0,z}
  239. };
  240. return new Matrix(m);
  241. }
  242.  
  243. class Vertex {
  244. Matrix p;
  245. Vector pos;
  246. Vector renderPos;
  247. Vertex (Vector pos_) {
  248. pos=pos_;
  249. }
  250.  
  251. Vertex () {}
  252.  
  253. void beforeDraw() {
  254. renderPos=pos;
  255. }
  256.  
  257. void multiplyMatrix (Matrix m) {
  258. renderPos = m.multiply(renderPos);
  259. }
  260.  
  261. void render() {
  262. multiplyMatrix(createTranslation(pos.pos[0],pos.pos[1],pos.pos[2],renderPos));
  263. float[][] P = {
  264. {1/renderPos.pos[2],0,0},
  265. {0,1/renderPos.pos[2],0},
  266. {0,0,1}
  267. };
  268. p = new Matrix(P);
  269. multiplyMatrix(p);
  270. point(renderPos.pos[0],renderPos.pos[1]);
  271. }
  272. }
  273. class Object {
  274. Vertex[] vertexList;
  275. Vector rot = new Vector();
  276. Vector scl = new Vector();
  277. Object (Vertex[] vertexlist, Vector rot_, Vector scl_) {
  278. vertexList = new Vertex[vertexlist.length];
  279. vertexList = vertexlist;
  280. rot = rot_;
  281. scl = scl_;
  282. }
  283. void beforeDraw() {
  284. for (int i=0;i<vertexList.length+1;i++) {
  285. vertexList[i].beforeDraw();
  286. }
  287. }
  288. void render() {
  289. if (vertexList==null) {
  290. System.err.println("(!) Object can't be displayed!");
  291. return;
  292. }
  293. for (int i=0;i<vertexList.length;i++) {
  294. if (vertexList[i]==null) {
  295. System.err.println("(!) Vertex can't be displayed!");
  296. continue;
  297. }
  298. vertexList[i].multiplyMatrix(createRotation(rot.pos[0],rot.pos[1],rot.pos[2]));
  299. vertexList[i].multiplyMatrix(createScale(scl.pos[0],scl.pos[1],scl.pos[2]));
  300. vertexList[i].render();
  301. }
  302. }
  303. }
  304.  
  305. Object Cube;
  306.  
  307. void setup() {
  308. float[] Rot = {0,0,0};
  309. float[] Scl = {50,50,50};
  310.  
  311. float[] a = {1,-1,1};
  312. float[] b = {1,-1,-1};
  313. float[] c = {-1,-1,1};
  314. float[] d = {-1,-1,-1};
  315. float[] e = {1,1,1};
  316. float[] f = {1,1,-1};
  317. float[] g = {-1,1,1};
  318. float[] h = {-1,1,-1};
  319. Vertex[] vertexlist = new Vertex[8];
  320. vertexlist[0] = new Vertex(new Vector(a));
  321. vertexlist[1] = new Vertex(new Vector(b));
  322. vertexlist[2] = new Vertex(new Vector(c));
  323. vertexlist[3] = new Vertex(new Vector(d));
  324. vertexlist[4] = new Vertex(new Vector(e));
  325. vertexlist[5] = new Vertex(new Vector(f));
  326. vertexlist[6] = new Vertex(new Vector(g));
  327. vertexlist[7] = new Vertex(new Vector(h));
  328. Vector rot = new Vector(Rot);
  329. Vector scl = new Vector(Scl);
  330. Cube = new Object(vertexlist, rot, scl);
  331. }
  332.  
  333. void beginDraw() {
  334. Cube.beforeDraw();
  335. }
  336.  
  337. void draw() {
  338. background(50);
  339. Cube.render();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement