#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# How to use Java PathIterator or Serialization Methods (from Java) in a Processing sketch

edited November 2016

I am using the java.awt.Polygon in my processing sketch because I need its functions "contains" and "intersects" for my code. It's about a Pendulum simulation and the moving body of the pendulum is the Polygon that 'hits' objects and adjusts its motion to them. So I need to move and rotate my Polygon, and for this, after some research I did, I believe I need the PathIterator or the Serialization Methods from java ( https://docs.oracle.com/javase/7/docs/api/java/awt/geom/PathIterator.html OR https://docs.oracle.com/javase/7/docs/api/serialized-form.html#java.awt.Polygon). Can you please give me an example of how these functions can be used in Processing?

I am posting my code in case it helps to understand the problem. At the moment what I am trying is to know when the polygon hits the 'ground'. In the class ground I have the boolean intersects, but it always returns false, and I believe it is because the actual polygon doesn't move. Only its image moves.

``````Pendulum pendulum;
MyPolygon mypolygon;
Ground ground;

//MYPOLYGON
PVector[] points;
PVector massCenter;

//PENDULUM
PVector origin;
PVector location;

//GROUND
int wdth=260;
int ht=3;
int xRect=-wdth/2;
int yRect=15;

void setup() {
size(600, 400, P2D);

points=new PVector;
points= new PVector(0, 0);
points= new PVector(-50, 0);
points= new PVector(-50, -10);
points= new PVector(-75, -20);
points= new PVector(-75, -40);
points= new PVector(25, -40);
points= new PVector(25, -20);
points= new PVector(0, -10);

origin=new PVector(width/2, height/2);

mypolygon=new MyPolygon(points);
mypolygon.createGrid();
massCenter= mypolygon.centerOfMass();

pendulum=new Pendulum(origin, massCenter, points);

ground=new Ground(xRect, yRect, wdth, ht);
}

void draw() {
background(0);

mypolygon. drawGrid();
mypolygon.drawCenterOfMass();
mypolygon.drawInitialShape(50);

pendulum.debug();
pendulum.update();
pendulum.display();

ground.intersection();
ground.display();

}

class MyPolygon {

int scl=5;
int rows=50;
int cols=40;
float xAverage;
float yAverage;

java.awt.Polygon p;
PVector[] pts;
ArrayList <PVector> grid;

MyPolygon(PVector[] pts_) {

pts=pts_;

p = new java.awt.Polygon();

for (PVector a : pts) {
}

grid=new ArrayList<PVector>();
}

void createGrid() {

for (int i=-rows; i<rows; i++) {
for (int j=-cols; j<cols; j++) {
PVector k=new PVector(i*scl, j*scl);
if (p.contains(i*scl, j*scl)) {
}
}
}
}

void drawGrid() {

pushMatrix();
translate(origin.x, origin.y);
fill(0, 0, 200);
noStroke();

for (PVector v : grid) {
ellipse(v.x, v.y, 2, 2);
}

popMatrix();
}

PVector centerOfMass() {
int xcount=0;
int ycount=0;

for (int i=0; i<grid.size(); i++) {
xcount+=grid.get(i).x;
ycount+=grid.get(i).y;
}

xAverage=xcount/grid.size();
yAverage=ycount/grid.size();

PVector r=new PVector (xAverage, yAverage);
return r;
}

void drawCenterOfMass() {

fill(0, 100, 200);
ellipse(massCenter.x, massCenter.y, 10, 10);
}

void drawInitialShape(float alpha) {

pushMatrix();
translate(origin.x, origin.y);
fill(255, alpha);
beginShape();
for (PVector a : points) {

vertex(a.x, a.y);
}
endShape();
popMatrix();
}

}

}

class Pendulum {

PVector[] pts;

PVector penOrigin;
PVector penLocation;
float len;

float a;
float aVel;
float aAcc;

float angleThickness;

Pendulum(PVector origin_, PVector location_, PVector[] pts_) {

pts=pts_;

penOrigin=origin_.get();
penLocation=location_.get();
len=dist(penOrigin.x, penOrigin.y, penLocation.x, penLocation.y);

PVector diff=new PVector();
diff=penLocation;
diff.sub(penOrigin);

angleThickness=a;

aVel=0;
aAcc=0;
}

void debug() {
println ("P.origin =" + penOrigin + "P. location= " + penLocation + "angle a=" + a);
}

void update() {

aAcc=-0.0001*len*sin(a);
aVel+=aAcc;
aVel*=0.99;
a+=aVel;

penLocation.set(sin(a)*len, cos(a)*len, 0);
}

void display() {

pushMatrix();
translate(origin.x, origin.y);

fill(255, 0, 200);
ellipse(0, 0, 20, 20);
ellipse(penLocation.x, penLocation.y, 20, 20);

pushMatrix();
rotate(-a + (PI/2 -angleThickness));
fill(255, 100);
beginShape();
for (PVector a : pts) {

vertex(a.x, a.y);
}
endShape();
popMatrix();
popMatrix();
}
}

class Ground{

float x;
float y;
float w;
float h;

boolean touch=false;

Ground(float x_, float y_, float w_, float h_){
x=x_;
y=y_;
w=w_;
h=h_;
}

void display(){

pushMatrix();
translate(origin.x, origin.y);
fill(255);
rect(x,y ,w,h);
//rect(10,-200,460,30);
popMatrix();

}

void intersection() {

boolean c=mypolygon.p.intersects(x, y, w, h);
println(c);
}
}
``````
Tagged:

• I believe it is because the actual polygon doesn't move. Only its image moves.

You are right that is the reason.

You do not need the PathIterator or Serialization to do this you simply need to update the polygon points as the pendulum swings. To do this we use the java.awt.geom.AffineTransform class to transform the original polygon points by translating and rotating them as we do the same for the pendulum. The MyPolygon class has two arrays, one holding the original points and the second the transformed points. It also has an update method to perform the transformation. This method is called from the update method in the Pendulum class so they get updated together.

I have also lowered the ground to make it easier to test the code.

HTH

``````import java.awt.*;
import java.awt.geom.*;

Pendulum pendulum;
MyPolygon mypolygon;
Ground ground;

//MYPOLYGON
PVector[] points;
PVector massCenter;

//PENDULUM
PVector origin;
PVector location;

//GROUND
int wdth=260;
int ht=3;
int xRect=-wdth/2;
int yRect=85;

void setup() {
size(600, 400, P2D);

points=new PVector;
points= new PVector(0, 0);
points= new PVector(-50, 0);
points= new PVector(-50, -10);
points= new PVector(-75, -20);
points= new PVector(-75, -40);
points= new PVector(25, -40);
points= new PVector(25, -20);
points= new PVector(0, -10);

origin=new PVector(width/2, height/2);

mypolygon=new MyPolygon(points);
mypolygon.createGrid();
massCenter= mypolygon.centerOfMass();
pendulum=new Pendulum(origin, massCenter, points);

ground=new Ground(xRect, yRect, wdth, ht);
}

void draw() {
background(0);

mypolygon. drawGrid();
mypolygon.drawCenterOfMass();
mypolygon.drawInitialShape(50);

pendulum.debug();
pendulum.update();
pendulum.display();

ground.intersection();
ground.display();
}

class MyPolygon {

int scl=5;
int rows=50;
int cols=40;
float xAverage;
float yAverage;

Polygon p;
PVector[] pts;
ArrayList <PVector> grid;

float[] orgPoints;
float[] transformedPoints;

MyPolygon(PVector[] pts_) {
pts=pts_;
// Create an array of original points and an array of transformed points.
// Initially the polygon is untransford.
orgPoints = new float[pts_.length * 2];
transformedPoints = new float[pts_.length * 2];
for (int n = 0; n < pts_.length; n++) {
orgPoints[n] = transformedPoints[n] = pts_[n].x;
orgPoints[n+1] = transformedPoints[n+1] = pts_[n].y;
}
p = makePolygon(transformedPoints);
grid=new ArrayList<PVector>();
}

void update(float x, float y, float ang) {
AffineTransform af = new AffineTransform();
af.translate(x, y);
af.rotate(ang);
af.transform(orgPoints, 0, transformedPoints, 0, orgPoints.length/2);
p = makePolygon(transformedPoints);
}

Polygon makePolygon(float[] pts) {
Polygon p = new Polygon();
for (int n = 0; n < pts.length; n += 2) {
}
return p;
}

void createGrid() {

for (int i=-rows; i<rows; i++) {
for (int j=-cols; j<cols; j++) {
PVector k=new PVector(i*scl, j*scl);
if (p.contains(i*scl, j*scl)) {
}
}
}
}

void drawGrid() {

pushMatrix();
translate(origin.x, origin.y);
fill(0, 0, 200);
noStroke();

for (PVector v : grid) {
ellipse(v.x, v.y, 2, 2);
}

popMatrix();
}

PVector centerOfMass() {
int xcount=0;
int ycount=0;

for (int i=0; i<grid.size(); i++) {
xcount+=grid.get(i).x;
ycount+=grid.get(i).y;
}

xAverage=xcount/grid.size();
yAverage=ycount/grid.size();

PVector r=new PVector (xAverage, yAverage);
return r;
}

void drawCenterOfMass() {
fill(0, 100, 200);
ellipse(massCenter.x, massCenter.y, 10, 10);
}

void drawInitialShape(float alpha) {
pushMatrix();
translate(origin.x, origin.y);
fill(255, alpha);
beginShape();
for (PVector a : points) {
vertex(a.x, a.y);
}
endShape();
popMatrix();
}

}

class Pendulum {

PVector[] pts;

PVector penOrigin;
PVector penLocation;
float len;

float a;
float aVel;
float aAcc;

float angleThickness;

Pendulum(PVector origin_, PVector location_, PVector[] pts_) {
pts=pts_;

penOrigin=origin_.get();
penLocation=location_.get();
len=dist(penOrigin.x, penOrigin.y, penLocation.x, penLocation.y);

PVector diff=new PVector();
diff=penLocation;
diff.sub(penOrigin);

angleThickness=a;

aVel=0;
aAcc=0;
}

void debug() {
println ("P.origin =" + penOrigin + "P. location= " + penLocation + "angle a=" + a);
}

void update() {
aAcc=-0.0001*len*sin(a);
aVel+=aAcc;
aVel*=0.99;
a+=aVel;
penLocation.set(sin(a)*len, cos(a)*len, 0);
// Update the polygon vertices for the pendulum position
mypolygon.update(sin(a)*len, cos(a)*len, a);
}

void display() {
pushMatrix();
translate(origin.x, origin.y);

fill(255, 0, 200);
ellipse(0, 0, 20, 20);
ellipse(penLocation.x, penLocation.y, 20, 20);

pushMatrix();
rotate(-a + (PI/2 -angleThickness));
fill(255, 100);
beginShape();
for (PVector a : pts) {

vertex(a.x, a.y);
}
endShape();
popMatrix();
popMatrix();
}
}

class Ground {
float x;
float y;
float w;
float h;

boolean touch=false;

Ground(float x_, float y_, float w_, float h_) {
x=x_;
y=y_;
w=w_;
h=h_;
}

void display() {
pushMatrix();
translate(origin.x, origin.y);
fill(255);
rect(x, y, w, h);
//rect(10,-200,460,30);
popMatrix();
}

void intersection() {
boolean c=mypolygon.p.intersects(x, y, w, h);
println(c);
}
}
``````