1

I want to ask a question about Processing. I have a user defined object class, Point, and it have two data members x and y both declared as float. I have another class Recognizer. I need to create the constructor of Recognizer and inside it define the object of Point[] point0, Point[] point1, etc... dynamically.

Point [] point0={new Point(137,139),new Point(50,63),..., new Point(78,5)};
so is point1[],point2[],...,pointn[]

How else can I add the values into point0 having the values of corresponding x and y coordinate.

Also note that the size of each object point1,point2,..., pointn are different.

How can I achieve the above goal?

7
  • 1
    You either create an array or an ArrayList and populate it in a loop. I suggest you do this as you read an external text file which contains the points you need. Commented Sep 4, 2013 at 7:29
  • Please pick a language, it doesn't make sense give answers in languages you are not using. Commented Sep 4, 2013 at 7:30
  • @PeterLawrey. I had specified Processing. I need an altenate method instead of adding the points using the code "Point [] point0={new Point(137,139),new Point(50,63),..., new Point(78,5)};" Commented Sep 4, 2013 at 7:44
  • @PeterLawrey Processing is an open source programming language and integrated development environment (IDE) built for the electronic arts, new media art, and visual design communities with the purpose of teaching the fundamentals of computer programming in a visual context, and to serve as the foundation for electronic sketchbooks.The language builds on the Java language, but uses a simplified syntax and graphics programming model. Commented Sep 4, 2013 at 7:55
  • In that case, I would remove the other languages, as this is confusing and possibly the reason no one else has commented. If you are concerned no one will answer, I would ask how to do it in Java (which you say it is based on) and translate it yourself. Commented Sep 4, 2013 at 7:58

1 Answer 1

1

How about using a 2 dimensional array ?

Point [] points0={new Point(137,139),new Point(50,63),new Point(78,5)};
Point [] points1={new Point(147,139),new Point(60,63),new Point(79,5)};
Point [] points2={new Point(157,139),new Point(70,63),new Point(80,5)};

void setup(){
  Point[][] pointLists = {points0,points1,points2};
  Recognizer r = new Recognizer(pointLists);
}

class Point{
  float x,y;
  Point(float x,float y){
    this.x = x;
    this.y = y;
  }
  String toString(){
    return "Point("+x+","+y+")";
  }
}
class Recognizer{
  Point[][] data;
  Recognizer(Point[][] data){
    this.data = data;
    for(int i = 0 ; i < data.length; i++){
      println("processing points list" + i);
      for(int j = 0; j < data[i].length; j++){
        println("processing point["+j+"] of list " + i + " : "+data[i][j]);
      }
    }
  }
}

and the ArrayList version:

import java.util.Arrays;

ArrayList<Point> points0 = new ArrayList<Point>(Arrays.asList(new Point[]{new Point(137,139),new Point(50,63),new Point(78,5)}));
ArrayList<Point> points1 = new ArrayList<Point>(Arrays.asList(new Point[]{new Point(147,139),new Point(60,63),new Point(79,5)}));
ArrayList<Point> points2 = new ArrayList<Point>(Arrays.asList(new Point[]{new Point(157,139),new Point(70,63),new Point(80,5)}));

void setup(){
  ArrayList<ArrayList<Point>> pointLists = new ArrayList<ArrayList<Point>>();
  pointLists.add(points0);
  pointLists.add(points1);
  pointLists.add(points2);
  Recognizer r = new Recognizer(pointLists);
}

class Point{
  float x,y;
  Point(float x,float y){
    this.x = x;
    this.y = y;
  }
  String toString(){
    return "Point("+x+","+y+")";
  }
}
class Recognizer{
  ArrayList<ArrayList<Point>> data;
  Recognizer(ArrayList<ArrayList<Point>> data){
    this.data = data;
    for(int i = 0 ; i < data.size(); i++){
      println("processing points list" + i);
      for(int j = 0; j < data.get(i).size(); j++){//write this in a nicer way, too many get calls, might want a reusable variable here
        println("processing point["+j+"] of list " + i + " : "+data.get(i).get(j));
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

how to implement the same useing an array list?
@MELWIN see updated answer, feel free to mark/vote if this is useful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.