i'm doing a project for a research course involving shapes intersecting. I've written code which should fill the intersection of two shapes. However it is giving inconsistent results.
Here is the code so far:
import geomerative.*;
SimplePolygon pent;
SimplePolygon quad;
SimplePolygon tri;
SimplePolygon line;
SimplePolygon circle;
int count=0;
void setup()
{
size(600, 600);
smooth();
//fill(80);
//noStroke();
noFill();
background(255);
RG.init(this);
pent = new SimplePolygon(5);
quad = new SimplePolygon(4);
tri = new SimplePolygon(3);
line = new SimplePolygon(2);
circle = new SimplePolygon(0);
}
void draw() {
background(255);
//println("mouse X is " + mouseX + " & mouse Y is " + mouseY);
pent.setPoints();
pent.drawShape();
quad.setPoints();
quad.drawShape();
boolean inters = doPolysIntersect(quad, tri);
println("---------------------------------------------------------");
println(inters + " " + count + " ");
printPolyPoints(quad);
printPolyPoints(tri);
if(inters) {
RShape rs = getPolyIntersection(quad, tri);
drawPolyIntersection(rs);
}
tri.setPoints();
tri.drawShape();
line.setPoints();
line.drawShape();
circle.drawShape();
count++;
}
void mousePressed() {
pent.mPressed();
quad.mPressed();
tri.mPressed();
line.mPressed();
circle.mPressed();
}
void mouseReleased() {
pent.mReleased();
quad.mReleased();
tri.mReleased();
line.mReleased();
circle.mReleased();
}
boolean doPolysIntersect (SimplePolygon poly1, SimplePolygon poly2) {
boolean intersect = poly1.shape.intersects(poly2.shape);
return intersect;
}
RShape getPolyIntersection (SimplePolygon poly1, SimplePolygon poly2) {
return poly1.shape.intersection(poly2.shape);
}
void drawPolyIntersection(RShape insect) {
fill(150);
insect.draw();
noFill();
}
void printPolyPoints(SimplePolygon poly) {
for (int i = 0; i < poly.points.length; i++) {
RPoint rp = poly.points[i];
print("P" + i + ": (" + rp.x + ", " + rp.y + ") .. ");
}
println();
}
class SimplePolygon {
RPoint points[];
int numSides;
float angle;
RShape shape;
boolean isRotating;
boolean isDragging;
boolean isPointDragging;
int draggingPointIndex;
SimplePolygon(int n) {
numSides = n;
points = new RPoint[numSides];
shape = new RShape();
//set default shapes for each n - 3, 4, 5
if (numSides == 5){
initPentagonPts();
}
else if (numSides == 4){
initQuadPts();
}
else if (numSides == 3){
initTriPts();
}
else if (numSides == 2) {
initLinePts();
}
else if (numSides == 0) {
initCircle();
}
}
void initPentagonPts() {
points[0] = new RPoint(100.0, 100.0);
points[1] = new RPoint(200.0, 100.0);
points[2] = new RPoint(250.0, 170.0);
points[3] = new RPoint(225.0, 200.0);
points[4] = new RPoint(100.0, 200.0);
}
void initQuadPts() {
points[0] = new RPoint(170.0, 60.0);
points[1] = new RPoint(275.0, 5.0);
points[2] = new RPoint(300.0, 125.0);
points[3] = new RPoint(170.0, 125.0);
}
void initTriPts() {
points[0] = new RPoint(275.0, 75.0);
points[1] = new RPoint(290.0, 250.0);
points[2] = new RPoint(170.0, 225.0);
}
void initLinePts() {
points[0] = new RPoint(150.0, 25.0);
points[1] = new RPoint(150.0, 75.0);
}
void initCircle() {
shape = RShape.createCircle(80, 80, 100);
}
void setPoints() {
shape.addMoveTo(points[0]);
for(int i = 1; i< numSides; i++) {
shape.addLineTo(points[i]);
}
if (numSides > 2) { // as long as not a line
shape.addLineTo(points[0]); // close the shape
}
}
void drawShape() {
// move with mouse
if (isDragging) {
if (numSides > 0) {
moveShape(mouseX, mouseY);
}
else if (numSides == 0) {
shape = RShape.createCircle(mouseX, mouseY, 100);
}
}
if (numSides > 0 && isPointDragging) {
movePoint(mouseX, mouseY);
}
drawCentre();
if (numSides > 0) {
drawPoints();
}
shape.draw();
}
void moveShape(float x, float y) {
float x_trans = shape.getCenter().x - x;
float y_trans = shape.getCenter().y - y;
println(x_trans + " " + y_trans);
for(int i = 0; i < numSides; i++) {
points[i].x -= x_trans;
points[i].y -= y_trans;
}
shape = new RShape();
setPoints();
}
void movePoint(float x, float y) {
points[draggingPointIndex].x = x;
points[draggingPointIndex].y = y;
shape = new RShape();
setPoints();
}
void drawCentre() {
RPoint ctr = shape.getCenter();
RShape ctr_circ = RShape.createCircle(ctr.x, ctr.y, 10);
fill(40);
ctr_circ.draw();
noFill();
}
void drawPoints() {
fill(150);
for (int i = 0; i < numSides; i++) {
RShape pt_circ = RShape.createCircle(points[i].x, points[i].y, 10);
pt_circ.draw();
}
noFill();
}
// mouse events
void mPressed() {
if (dist(mouseX, mouseY, shape.getCenter().x, shape.getCenter().y) < 10) {
isDragging = true;
}
for (int i = 0; i < numSides; i++ ) {
if(dist(mouseX, mouseY, points[i].x, points[i].y) < 10) {
draggingPointIndex = i;
isPointDragging = true;
}
}
}
void mReleased() {
isDragging = false;
isPointDragging = false;
}
} //end class
you can see in the draw() function that i'm printing a Boolean (to console) of whether the quad and triangle shapes intersect (4-sided and 3 sided shapes). Note - you can move each shape by dragging their "centre point".
When they are separate (not intersecting), it consistently returns "false" which is what it should.
However when they are intersecting, it alternates between "true" and "false" on each draw() loop iteration.
I'm writing the points of both shapes to the console on each iteration, and the points themselves don't change. However the Boolean result does change, alternating between "true" and "false".
i'm unable to get my head around why this is happening. Any help to shed some light on this would be greatly appreciated.
Also the shapes as drawn to the screen seem to flicker a little bit too. Could this be a related problem..?
p.s. sorry for the messy code, i'm not much of a programmer :)
cheers from "down under"!