help with confining points to the screen area
in
Programming Questions
•
23 days ago
amnon, in posting to a question of mine, offered this beautiful code.
https://forum.processing.org/topic/controlling-the-length-of-line-from-x-y-to-a-point-which-is-randomly-chosen
it appears a little more than half-way down the page.
Because I am learning, I decided to work with the code piece by piece to see if I could learn what it was doing.
I'm leaving out all the camera stuff which is way beyond me. But, I ran into a problem when I wanted to confine, to the processing screen, the lines being drawn.
I first tried constrain with this code:
and that appeared to do nothing.
I then tried the forceful brute math method:
And still they disappear.
Although the line usually comes back on screen fairly fast, my main issue is with the immediate start up of the sketch -- it VERY often is off screen for quite some time and I stop and restart. But I would like to ALWAYS have the line start on the screen area.
I've no idea what's going on. Here is the full sketch:
https://forum.processing.org/topic/controlling-the-length-of-line-from-x-y-to-a-point-which-is-randomly-chosen
it appears a little more than half-way down the page.
Because I am learning, I decided to work with the code piece by piece to see if I could learn what it was doing.
I'm leaving out all the camera stuff which is way beyond me. But, I ran into a problem when I wanted to confine, to the processing screen, the lines being drawn.
I first tried constrain with this code:
- // constrain(p.x, 0, width-20);
- // constrain(p.y, 0, height-20);
- // constrain(p.z, 0, height);
and that appeared to do nothing.
I then tried the forceful brute math method:
- if (p.x >= width) {p.x=width-10;}
- if (p.y >= height){ p.y=height-10;}
- if (p.x < 0) {p.x=10;}
- if (p.y < 0){ p.y=10;}
And still they disappear.
Although the line usually comes back on screen fairly fast, my main issue is with the immediate start up of the sketch -- it VERY often is off screen for quite some time and I stop and restart. But I would like to ALWAYS have the line start on the screen area.
I've no idea what's going on. Here is the full sketch:
- // a selection from amnon's 3D line code
- ArrayList <PVector> points = new ArrayList <PVector> ();
- PVector c = new PVector();
- void setup() {
- size(700, 500, P3D);
- background(0);
- colorMode(HSB, 500, 100, 100);
- points.add( new PVector() );
- frameRate(5);
- smooth(8);
- strokeWeight(2);
- noFill();
- }
- void draw() {
- background(0);
- int index=0;
- beginShape();
- for (PVector p : points) {
- stroke((++index+frameCount)%500, 100, 100);
- if (p.x >= width) { p.x=width-10; }
- if (p.y >= height) { p.y=height-10; }
- if (p.x < 0) { p.x=10; }
- if (p.y < 0) { p.y=10; }
- curveVertex(p.x, p.y, p.z);
- }
- endShape();
- // new point at specified range
- PVector n = PVector.random3D();
- n.mult(100);
- n.add(points.get(points.size()-1));
- points.add( n );
- if (mousePressed == true) {
- saveFrame("mylines-####.tif");
- }
- }
1