Putting point object into new ArrayList, when coordintes are equal

Hi,

I have a question about two ArrayLists of my script.

There are two ArraLists:

for(int i = 0; i < inputPolys.size(); i++){
            WB_Polygon2D a = inputPolys.get(i);
            for(int j = 0; j < a.points.length; j++ ){
            pointsInput1.add(a.points[j]);
            }//for
        }//for

for(int i = 0; i < intersectPolys.size(); i++){
            WB_Polygon2D a = intersectPolys.get(i);
            for(int j = 0; j < a.points.length; j++ ){
            pointsInput2.add(a.points[j]);
            }//for
        }//for

The script is dealing with polygons - inputPolys and intersected polygons - intersectPolys. There are N polygons in inputPolys, and from 0 to n-1 polygons in intersectPolys ArrayList (this arrayList changes each frame - the number of objects changes).

What I am doing in these double for() loops is getting the points of polygons and adding them to new ArrayLists
a) pointsInput1 - for inputPolys polygons
b) pointsInput2 - for changing intersectPolys polygons.

Then I want two compare if the location of pointsInput1 (a.points[i].x, a.points[i].y) and pointsInput2(b.points[j].x, b.points[j].y) are the same.

But this is the problem I have.

How to write a code when I want to compare one point of first arraylist with all the other points from second arrayList?

Thanks, Petras

Tagged:

Answers

  • edited October 2013

    Although it got nothing to do w/ your code, I think the algorithm used from the code below
    to determine whether a Dot is near to each other can be food for thought: :-\"

    /** 
     * Moving Dots (v2.26)
     * by Josem.93 (2013/Aug)
     * mod GoToLoop
     * 
     * forum.processing.org/topic/interaction-among-objects-in-an-array
     * forum.processing.org/topic/gradual-movement-within-a-for-loop
     * 
     * studio.processingtogether.com/sp/pad/export/ro.9s0026dE7B8v-/latest
     */
    
    final static int NUM = 30, FPS = 60;
    final static Dot[] dots = new Dot[NUM];
    
    boolean isPaused;
    
    void setup() {
      size(800, 600);
      frameRate(FPS);
      noSmooth();
      stroke(Dot.COLOUR);
      fill(Dot.COLOUR);
    
      for (int i = 0; i != NUM; dots[i++] = new Dot());
    }
    
    void draw() {
      background(-1);
      for (int i = 0; i != NUM; connectPoints(i++))   dots[i].script();
    }
    
    void mousePressed() {
      if (isPaused = !isPaused)   noLoop();
      else                          loop();
    }
    
    void keyTyped() {
      mousePressed();
    }
    
    final static void connectPoints(int i) {
      for (int z = i+1; z != NUM; z++)
        if ( dots[i].isNear(dots[z]) )   dots[i].drawLine(dots[z]);
    }
    
    class Dot {
      final static short DIM = 5, MIN_DIST = 30, MAX_SPD = 5;
      final static color COLOUR = 0100;
    
      float x, y;
      float spx = random(-MAX_SPD, MAX_SPD);
      float spy = random(-MAX_SPD, MAX_SPD);
    
      Dot() {
        x = width>>1;
        y = height>>1;
      }
    
      void script() {
        move();
        display();
      }
    
      void move() {
        if ((x += spx) > width  | x < 0)  spx *= -1;
        if ((y += spy) > height | y < 0)  spy *= -1;
      }
    
      void display() {
        ellipse(x, y, DIM, DIM);
      }
    
      boolean isNear(Dot other) {
        //return dist(x, y, other.x, other.y) < MIN_DIST;
        return sq(other.x - x) + sq(other.y - y) < MIN_DIST*MIN_DIST;
        //return abs(other.x - x) < MIN_DIST && abs(other.y - y) < MIN_DIST;
      }
    
      void drawLine(Dot other) {
        line(x, y, other.x, other.y);
      }
    }
    
  • edited October 2013

    Dear GoToLoop,

    I see that the script you posted is dealing with class Dot. Maybe this is a problem I have as I am not very good at using classes.

    Could you review my script? It is actually all about ArrayLists and it uses Hemesh library. At top of the script there are 3 triangles initialized, then they are put into inputPolys ArrayList, then intersected polygons are put into intersectPolys ArrayList.

    Then I am dealing with the current problem of comparing points of each list. I found the idea how to compare if each point is in the same location. You could look at function called "substractManyfromOne". But still happened another problem. I got at the same time 9 outcomes (not 1 as I need) in the arrayList "inputPoints3" --> the outcome is showed on display by println function. This happened because, there is for loop that is changing like that (1 object, 2 objects,... 9 objects).

    Do you know how to solve it?

    import processing.core.PApplet;
    import wblut.math.*;
    import wblut.processing.*;
    import wblut.core.*;
    import wblut.hemesh.*;
    import wblut.geom.*;
    
    import java.util.ArrayList;
    import java.util.List;
    
    WB_Render render;
    
    public void setup() {
    
      size(1280, 720, OPENGL);
      smooth();
    
      render = new WB_Render(this);
    }// setup
    
    public void draw() {
    
      background(255);
      noStroke();
      noFill();
    
      int dim = 200;
    
      //ArrayList of inputpolys
      ArrayList<WB_Polygon2D> inputPolys = new ArrayList<WB_Polygon2D>();
      ArrayList<WB_Point2d> points = new ArrayList<WB_Point2d>();
    
      // Initialize inputPoly 1
      points.add(new WB_Point2d(width / 2, height / 3 - dim));
      points.add(new WB_Point2d(width / 2 + dim, height / 2 + dim / 2));
      points.add(new WB_Point2d(width / 2 - dim, height / 2 + dim / 2));
      inputPolys.add(new WB_Polygon2D(points));
      // clear points
      points.clear();
      // Initialize inputPoly 2
      points.add(new WB_Point2d(mouseX, mouseY - dim));
      points.add(new WB_Point2d(mouseX + dim, mouseY + dim / 2));
      points.add(new WB_Point2d(mouseX - dim, mouseY + dim / 2));
      inputPolys.add(new WB_Polygon2D(points));
      // clear points
      points.clear();
      // Initialize inputPoly 3
      points.add(new WB_Point2d(width / 4, height / 3 - dim));
      points.add(new WB_Point2d(width / 5 + dim, height / 5 + dim / 2));
      points.add(new WB_Point2d(width / 5 - dim, height / 5 + dim / 2));
      inputPolys.add(new WB_Polygon2D(points));
      // clear points
      points.clear();
    
    
      for (WB_Polygon2D p : inputPolys) {
        fill(0, 10);
        render.drawPolygon2D(p);
      }
    
      //end of inputpolys
    
      stroke(0);
      fill(0, 50);
    
      List <WB_Polygon2D> intersectPolys = new ArrayList <WB_Polygon2D>();
      List<WB_Point2d> pointsInput1 = new ArrayList <WB_Point2d>();
      List<WB_Point2d> pointsInput2 = new ArrayList <WB_Point2d>();
      List<WB_Point2d> pointsInput3 = new ArrayList <WB_Point2d>();
      List <WB_Polygon2D> polyBig = new ArrayList <WB_Polygon2D>();
      List <WB_Polygon2D> polySmall = new ArrayList <WB_Polygon2D>();
      List <WB_Polygon2D> polyTest = new ArrayList <WB_Polygon2D>();
    
      for (int i = 0; i < inputPolys.size(); i++) {
        WB_Polygon2D a = inputPolys.get(i);
        for (int j = i + 1; j < inputPolys.size(); j++) {
          WB_Polygon2D b = inputPolys.get(j);
          intersectPolys.addAll(WB_Polygon2D.intersection(a, b));
        }//for
      }//for
    
      for (int i = 0; i < inputPolys.size(); i++) {
        WB_Polygon2D a = inputPolys.get(i);
        for (int j = 0; j < a.points.length; j++ ) {
          pointsInput1.add(a.points[j]);
          polyBig.add(new WB_Polygon2D(pointsInput1));
        }//for
      }//for
    
      for (int i = 0; i < intersectPolys.size(); i++) {
        WB_Polygon2D a = intersectPolys.get(i);
        for (int j = 0; j < a.points.length; j++ ) {
          pointsInput2.add(a.points[j]);
          polySmall.add(new WB_Polygon2D(pointsInput2));
        }//for
      }//for
    
      for (int i = 0; i < pointsInput1.size(); i++) {
        pointsInput3.addAll(substractManyFromOne(pointsInput1.get(i), 
        pointsInput2));
        println("frameCount " + frameCount + " result " + pointsInput3);
        //polyTest.add((WB_Polygon2D) pointsInput3);
      }// for
    
    
      for (WB_Polygon2D p : polyTest) {
        render.drawPolygon2D(p);
      }//for
    
      stroke(0);
      fill(0, 10);
    
      for (WB_Polygon2D p : intersectPolys) {
    
        //render.drawPolygon2D(p);
        render.drawPolygon2DVertices(p, 5);
      }//for
      // end of intersectPolys
    }// draw
    
    List<WB_Point2d> substractManyFromOne(WB_Point2d inputPt, List<WB_Point2d> intPt) {
      // this will collect all the pieces that remain from "one" after subtracting "many"
      List<WB_Point2d> eachPtinputPt = new ArrayList<WB_Point2d>();
      //adding points (arraylist list grows like a,b,c,d,e,f,g,h,i points)
      eachPtinputPt.add(inputPt); 
      //println(eachPtinputPt);
    
      List <WB_Point2d> AllFragmentsMinusOther = new ArrayList <WB_Point2d> ();
      for (WB_Point2d EintPt:intPt) {//enter the intersection list
    
        for (WB_Point2d eachPt : eachPtinputPt) {
    
          if (EintPt.x == eachPt.x && EintPt.y == eachPt.y) {
    
            AllFragmentsMinusOther.add(EintPt);
          }
        }//for
      }//for
      //if(AllFragmentsMinusOther != null){
      return AllFragmentsMinusOther;
    }// ArrayList substractManyFromOne  
    
  • Answer ✓

    Dunno if I'd be able to help you on this. I'm not very good at using 3rd-party libraries yet! 3:-O
    Anyways, to raise the chances for some1 to help you out, you should at least post your code well formatted! [..]
    After posting it, select it and press the C button (4th button).

  • Actually, it does no have much with 3rd-party library Hemesh. As it is dealing with a few simple classes and functions. If you have the Hemesh or could install it and take a look how my script works, it would be a big help for me:)

    Thanks.

Sign In or Register to comment.