Code Block |
---|
int gridWidth = 40; int gridHeight = 40; int screenWidth = 700; int screenWidthscreenHeight = 700; int distX; int screenHeight = 700 distY; PVector reactorPosition; ArrayList <VectorLine> vectLineList = new ArrayList<VectorLine>(); void setup() { reactorPosition = new PVector(0, 0); size(700, 700); background(255); stroke(0); int xOffsetdistX = width/gridWidth+1; int yOffsetdistY = height/gridHeight+1; for (int distXi = 0; i<gridWidth; i++ ) { for (int j = 0; j<gridHeight; j++ ) { vectLineList.add(new VectorLine(i*distX, j*distY)); } } }; PVector reactorPositionvoid draw() { background(255); ArrayListfor <VectorLine>(int vectLineListi = new ArrayList<VectorLine>(); 0; i<vectLineList.size(); i++ ) { vectLineList.get(i).draw(reactorPosition); }; }; void setupmouseMoved() { reactorPosition.x = mouseX; reactorPosition.y = mouseY; }; class VectorLine { PVector _pos; PVector _vector; float reactorScaler = .07; VectorLine(int x, int y) { _vector = new PVector(0, 05); size(700, 700);_pos = new PVector(x, y); _vector.add(_pos); } void draw(PVector reactor) { PVector _reactor = reactor.copy(); float background(255);reactorDistance = dist(_reactor.x, _reactor.y, _pos.x, _pos.y); float scaler = reactorDistance*reactorScaler; smooth(_reactor.sub(_pos); // subtract VectorLines position from the reactors position, this effectively gives int xOffset = floor(width/2-(distX*gridWidth/2)); int yOffset = floor(height/2-(gridHeight*distY/2)); a reactor coordinate relative to our VectorLine coordinate _vector = _reactor.copy(); _vector.normalize(); _vector.rotate(PI/4); _vector.mult(scaler); _vector.add(_pos); // line(_pos.x, _pos.y, _vector.x, _vector.y); ellipse(_vector.x, _vector.y, 5, 5); } } |
Example
This is an example solution for the exercises to create a tear-drop form.
Code Block |
---|
int gridWidth = 20; int gridHeight = 20; int screenWidth = 700; int screenHeight = 700; int distX; int distY; PVector reactorPosition; ArrayList <VectorLine> vectLineList = new ArrayList<VectorLine>(); void setup() { reactorPosition = new PVector(0, 0); size(700, 700); int distX = width/gridWidth+1; int distY = height/gridHeight+1; for (int i = 0; i<gridWidth; i++ ) { for (int j = 0; j<gridHeight; j++ ) { vectLineList.add(new VectorLine(i*distX, j*distY)); } } }; void draw() { background(255); for (int i = 0; i<vectLineList.size (); i++ ) { vectLineList.get(i).draw(reactorPosition); }; }; void mouseMoved() { reactorPosition.x = mouseX; reactorPosition.y = mouseY; }; class VectorLine { PVector _pos; PVector _vector; float reactorScaler = .071; VectorLine(int x, int y) { _vector = new PVector(0, 5); _pos = new PVector(x, y); _vector.add(_pos); } void draw(PVector reactor) { float reactorDistance = dist(reactor.x, reactor.y, _pos.x, _pos.y); float scaler = reactorDistance*reactorScaler; PVector _reactor = reactor.copy(); stroke(0_reactor.sub(_pos); _vector = _reactor.sub(_poscopy(); _vector.normalize(); _vector.rotate(PI/4); PVector _vectorpointTwo = _reactorvector.copy(); _pointTwo.rotate(PI/2); _vectorpointTwo.normalizemult(8); PVector _pointTree = _pointTwo.copy(); _vectorpointTree.rotate(PI/42); PVector _pointFour = _pointTree.copy(); // _pointFour.rotate(PI/2); _pointTwo.add(_pos); _pointTree.add(_pos); _pointFour.add(_pos); _vector.mult(scaler); _vector.add(_pos); noStroke(); fill(0); line beginShape(); curveVertex(_posvector.x, _posvector.y); curveVertex(_vector.x, _vector.y); curveVertex(_pointTwo.x, _pointTwo.y); _vector curveVertex(_pointTree.x, _pointTree.y); curveVertex(_pointFour.x, _pointFour.y); ellipsecurveVertex(_vector.x, _vector.y, 5, 5); curveVertex(_vector.x, _vector.y); }endShape(); } } |