Problem with wrap around method
in
Programming Questions
•
1 year ago
Hi guys,
I have another problem, well actually two.
1. I have been trying to design a wrap around method for some lines but I still am having a few issues
2. Every time I initialise another instant of my object I get the first object only shortened even though my variables are set to random
Below is the code I am using.
Thanks in advance,
Francis
- Curvy[] numOfCurveArray = {};
- void setup() {
- size(500, 300);
- background(255);
- smooth();
- strokeWeight(2);
- drawCurves();
- }
- //wrap around control statements REDESIGN THESE STATEMENTS!!!!!
- float wrapAroundX (float x) {
- if (x > width) {
- x = x % width;
- }
- if (x < 0) {
- x = width - (abs(x) % width);
- }
- return x;
- }
- float wrapAroundY (float y) {
- if (y > height) {
- y = y % height;
- }
- if (y < 0) {
- y = height - (abs(y) % height);
- }
- return y;
- }
- void mouseReleased() {
- drawCurves();
- }
- void drawCurves() {
- Curvy thisCurvy = new Curvy();
- thisCurvy.drawMe();
- numOfCurveArray = (Curvy[])append(numOfCurveArray, thisCurvy);
- }
- void draw() {
- background(255);
- for (int i = 0; i < numOfCurveArray.length; i ++) {
- Curvy thisCurvy = numOfCurveArray[i];
- thisCurvy.update();
- }
- }
- class Curvy {
- int lengthOfLine;
- int xstep;
- float yfinal;
- float ynoise;
- float xcoord, ycoord;
- ArrayList<Float> curveArrayListX=new ArrayList<Float>();
- ArrayList<Float> curveArrayListY=new ArrayList<Float>();
- Curvy() {
- lengthOfLine = int(random(width/4));
- xstep = 1;
- ynoise = random(1);
- xcoord = random(width);
- ycoord = random(height);
- curveArrayListX.add(xcoord);
- curveArrayListY.add(ycoord);
- for (int i = 1; i <=lengthOfLine; i += xstep) {
- xcoord += xstep;
- ycoord += noise(ynoise)*10 - 5;
- ynoise += 0.1;
- curveArrayListX.add(xcoord);
- curveArrayListY.add(ycoord);
- }
- }
- void drawMe() {
- for (int i = 0; i < curveArrayListX.size() - 1; i ++) {
- // line(curveArrayListX.get(i), curveArrayListY.get(i), curveArrayListX.get(i + 1), curveArrayListY.get(i + 1));
- line(wrapAroundX(curveArrayListX.get(i)) , wrapAroundY(curveArrayListY.get(i)), wrapAroundX(curveArrayListX.get(i + 1)) , wrapAroundY(curveArrayListY.get(i + 1)));
- }
- }
- // void drawMe() {
- // for (int i = 0; i < curveArrayListX.size() - 1; i ++) {
- // float xcob = curveArrayListX.get(i);
- // float xcoa = curveArrayListX.get(i + 1);
- // float ycob = curveArrayListY.get(i);
- // float ycoa = curveArrayListY.get(i + 1);
- // if (xcoa < width && ycoa < height) {
- // line(xcob, ycob, xcoa, ycoa);
- // } if (xcob > width && ycob < height) {
- // line(wrapAroundX(xcob), ycob, wrapAroundX(xcoa), ycoa);
- // } if (xcob < width && ycob > height) {
- // line(xcob, wrapAroundY(ycob), xcoa, wrapAroundY(ycoa));
- // } if (xcob > width && ycob > height) {
- // line(wrapAroundX(xcob), wrapAroundY(ycob), wrapAroundX(xcoa), wrapAroundY(ycoa));
- // }
- // }
- // }
- void update() {
- //move other way???
- float xmove = curveArrayListX.get(curveArrayListX.size() - 1) - curveArrayListX.get(0);
- float ymove = curveArrayListY.get(curveArrayListY.size() - 1) - curveArrayListY.get(0);
- float tempx = curveArrayListX.get(1);
- float tempy = curveArrayListY.get(1);
- curveArrayListX.add(tempx += xmove);
- curveArrayListY.add(tempy += ymove);
- curveArrayListX.remove(0);
- curveArrayListY.remove(0);
- drawMe();
- }
- }
1