Thank you so much again!!!
I just realized I cannot use library because I will using this .pde into website = processing js, but processing js does not support libraries...But still thanks for telling me that example, I have read it and really the things I want, I will remember it and I can use when I need it next time!!Thanks!!
(I have edit my reply. Earlier I just crazy so want to ask how to make those cube place in random position-_-then I realized I can simply use translate()...)
But here is another question, after I use translate() to place those cubes, can I get those shapes position? What I mean is, for example, the right face of the cube, I would like to get the coordinate of the right-top corner, can I get it? Because I am not changing the real coordinate of the shapes, but using translate() only...So if I want to get it, what can I do?(Because I need it to c
alculate the center point of the cube, in order to make cubes don't place to covered each other, and when they meet each other will
separate
etc..)
Here is my code...
- //Change the size of cube here
- //Strandard Size of Cube is 40, Center = 20, Max = 30, Min = 10
- int MinBoundary = 40;
- int CubeSize = MinBoundary * 4;
- int CubeCenter = MinBoundary * 2;
- int MaxBoundary = MinBoundary * 3;
- //Change the color alpha value here
- int alphaValue = 200;
- //Number of Cube to be genarate
- int CubeNum = 2;
- int counting = 0;
- float ranleftX, ranleftY;
- float ranrightX, ranrightY;
- float rantopX, rantopY;
- float My = 50;
- float Mx = 50;
- float speed = 1.0;
- boolean checked = false;
- Cubes [] c = new Cubes[CubeNum];
- float[] xPos = new float[CubeNum+1];
- float[] yPos = new float[CubeNum+1];
- void setup() {
- background(100);
- size(1000, 800);
- smooth();
- noStroke();
- int CubeRow = (width / (CubeSize += 5))-1;//1000 = 5
- int Zero = ((CubeSize ) * (CubeRow-1)); //640
- int row = 0;
- for (int i = 0; i < CubeNum; i++) {
- //Random value for the cube
- ranleftX = random(CubeSize);
- ranleftY = random(MinBoundary, MaxBoundary);
- ranrightX = random(CubeSize);
- ranrightY = random(MinBoundary, MaxBoundary);
- rantopX = random(MinBoundary, MaxBoundary);
- rantopY = random(MinBoundary, MaxBoundary);
- c[i] = new Cubes(ranleftX, ranleftY, ranrightX, ranrightY, rantopX, rantopY, 0, MinBoundary, MaxBoundary, CubeSize, CubeCenter);
- }
- }
- void draw() {
- if (counting < CubeNum) {
- float x = random(800);
- float y = random(700);
- println("x" + x + "y" + y);
- xPos[counting] = x;
- yPos[counting] = y;
- pushMatrix();
- translate(x, y);
- text(counting, 0, 0);
- c[counting].drawCube();
- popMatrix();
- counting++;
- }
- }
- //---------Class
- class Cubes {
- float rleftX;
- float rleftY;
- float rrightX;
- float rrightY;
- float rtopX;
- float rtopY;
- int minB;
- int start;
- int MinBoundary;
- int MaxBoundary;
- int CubeSize;
- int CubeCenter;
- //-----------------------Constructor
- Cubes(float rleftX, float rleftY, float rrightX, float rrightY, float rtopX, float rtopY, int start, int MinBoundary, int MaxBoundary, int CubeSize, int CubeCenter) {
- this.rleftX = rleftX;
- this.rleftY = rleftY;
- this.rrightX = rrightX;
- this.rrightY = rrightY;
- this.rtopX = rtopX;
- this.rtopY = rtopY;
- this.minB = minB;
- this.start = start;
- this.MinBoundary = MinBoundary;
- this.MaxBoundary = MaxBoundary;
- this.CubeSize = CubeSize;
- this.CubeCenter = CubeCenter;
- }
- //-----------------------Constructor
- //drawCube(ranleftX,ranleftY,ranrightX,ranrightY,rantopX,rantopY)
- void drawCube() {
- beginShape();
- //Left
- fill(#0071BC, alphaValue);
- beginShape(QUAD_STRIP);
- vertex(start, MinBoundary); //A
- vertex(start, MaxBoundary); //D
- vertex(rleftX, rleftY); //B cubecenter
- vertex(CubeCenter, CubeSize); //C
- endShape();
- //Right
- fill(#66AAD7, alphaValue);
- beginShape(QUAD_STRIP);
- vertex(rrightX, rrightY);
- vertex(CubeCenter, CubeSize);
- vertex(CubeSize, MinBoundary);
- vertex(CubeSize, MaxBoundary);
- endShape();
- //Top
- fill(#CCE3F2, alphaValue);
- beginShape(QUAD_STRIP);
- vertex(CubeCenter, start);
- vertex(start, MinBoundary);
- vertex(CubeSize, MinBoundary);
- vertex(rtopX, rtopX);
- endShape();
- endShape(CLOSE);
- }
- //-----------------------drawBall()
- }