"Strings"
in
Programming Questions
•
1 year ago
Not the datatype. I've been trying to recreate a sketch called strings that you can see
here. He explains the idea pretty well, and so I tried to recreate it. As far as I can tell, my sketch is finding the nearest not connected point accurately, but I think the problem is with my loop for checking for intersections.
I tried to comment to explain, and a lot of the code is the segment intersection function, but sorry for posting so much code.
- ArrayList dots=new ArrayList();
- ArrayList connections=new ArrayList();
- void setup() {
- size(500, 500);
- background(0);
- smooth();
- for (int i=0;i<100;i++) {
- dots.add(new d(random(0, width), random(0, height), i));
- }
- connections.add(new connection(-100, -100, -100, -100));//not a connection that will be checked
- }
- void draw() {
- for (int i=dots.size()-1;i>0;i--) {
- d d1 = (d) dots.get(i);
- d1.update();
- }
- }
- class d {//class for dot
- float x, y;
- boolean connected=false;
- int id;
- d(float cx, float cy, int cid) {
- x=cx;
- y=cy;
- id=cid;
- }
- void update() {
- float closest=100000;
- int closeid=0;
- boolean intersection=true;
- float x3=-101;
- float y3=-101;
- for (int i=dots.size()-1;i>0;i--) {//loop to find the closest dot
- d d1 = (d) dots.get(i);
- if (d1.id==id) {
- }
- else {
- if (d1.connected==false && connected==false) {
- if (dist(x, y, d1.x, d1.y)<closest) {//if it is closer than the closest one so far
- closest=dist(x, y, d1.x, d1.y);
- closeid=d1.id;
- x3=d1.x;
- y3=d1.y;
- }
- }
- }
- }
- for (int j=connections.size()-1;j>0;j--) {//loop through the connections and check to see if the connection with the closest dot intersects with any of them
- connection c1=(connection) connections.get(j);
- //get connection x's and y's
- float x1=c1.x1;
- float x2=c1.x2;
- float y1=c1.y1;
- float y2=c1.y2;
- if(x3!=-101 && y3!=-101){
- }else{
- if (segIntersection(x1, y1, x2, y2, x, y, x3, y3)!=null) {//check if they are intersecting
- intersection=true;
- }
- }
- }
- for (int i=dots.size()-1;i>0;i--) {//loop to pick out that dot and draw a line
- d d1 = (d) dots.get(i);
- if (d1.id==id) {//skip yourself
- continue;
- }
- if (d1.id==closeid && intersection==false) {//if you aren't intersecting and if the dot that has been looped to is the dot you're looking for
- stroke(0, 168, 255);
- line(x, y, d1.x, d1.y);
- connected=true;
- d1.connected=true;
- connections.add(new connection(x, y, d1.x, d1.y));//record the connection
- }
- }
- stroke(255, 70);
- point(x, y);
- }
- }
- class connection {//class to record all the connections that have been made
- float x1, y1, x2, y2;
- connection(float cx1, float cy1, float cx2, float cy2) {
- x1=cx1;
- y1=cy1;
- x2=cx2;
- y2=cx2;
- }
- }
- /**
- @author Ryan Alexander
- */
- PVector segIntersection(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4)
- {
- float bx = x2 - x1;
- float by = y2 - y1;
- float dx = x4 - x3;
- float dy = y4 - y3;
- float b_dot_d_perp = bx * dy - by * dx;
- if (b_dot_d_perp == 0) {
- return null;
- }
- float cx = x3 - x1;
- float cy = y3 - y1;
- float t = (cx * dy - cy * dx) / b_dot_d_perp;
- if (t < 0 || t > 1) {
- return null;
- }
- float u = (cx * by - cy * bx) / b_dot_d_perp;
- if (u < 0 || u > 1) {
- return null;
- }
- return new PVector(x1+t*bx, y1+t*by);
- }
1