I'm programming for the Meggy Jr. using the Arduino environment.
I am trying to use an array to store a series of coordinates to describe a shape. I used to use two separate arrays of ints, linked by index (e.g., DrawPx(xcoord[0], ycoord[0], Blue) and that worked okay but it was cumbersome.
Now instead, I'm trying to use a struct that I have defined like so:
struct Point{
int x;
int y;
};
Point s1 = {3,2};
Point s2 = {3,3};
Point myArray = {s1, s2}; // This works fine
The problem is that I am hoping to add a bunch of Points to the one array and I don't want to have to create each one as a separate variable prior to adding it to the array. What is the syntax for creating a new Point on the fly? Something like:
Point myArray = {new Point(3,2), new Point(3,3)}; // Doesn't work
I know this works in Java, I'm new to structs and am a little confused as to what the proper syntax would be (or if it's even possible).
Thanks for any insights,
--Doug.
Comments
struct Point
{
int x;
int y;
};
Point s1 = {3,4};
Point s2 = {4,4};
Point s3 = {5,4};
Point s4 = {6,4};
Point myArray[64] = {s1,s2,s3,s4}; //Shape coordinates
void drawShape()
{
for (int i = 0; i < 4; i++)
{
DrawPx(myArray[i].x,myArray[i].y,Red);
}
}
..so, that does work. I next want to declare a few shapes that each consist of about sixteen separate Point objects. I am reluctant to declare them as individual variables (Point s1 = {3,4}, Point s2 = (4,4}, etc.) just for the purposes of adding them to the array.
Is there a way to add Point objects to the array directly without taking the intermediary step of creating them as separate Point objects, each with their own variable name?
In Java, I would have to create a class with a constructor that takes parameters:
public class Point{
int myX;
int myY;
public Point (int x, int y){
myX = x;
myY = y;
}
}
but then I could initialize an array and add Points like this:
Point[] myArray = {new Point(3,4), new Point(4,4), new Point(5,4), new Point(6,4)};
Does Processing support anything similar?
Thanks,
--Doug.
struct Point myArray[3];
myArray[0] = (struct Point) {1,1};
myArray[1] = (struct Point) {4,5};
myArray[2] = (struct Point) {4,1};
--Doug.