Skip to content

Commit

Permalink
Added gridScale to snap data points to.
Browse files Browse the repository at this point in the history
  • Loading branch information
clothbot committed Aug 26, 2010
1 parent 0ed75f6 commit c30f63c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
19 changes: 17 additions & 2 deletions tests/AreaBooleans/Area2D.pde
Expand Up @@ -6,20 +6,35 @@ import java.awt.geom.PathIterator;
import java.awt.Polygon;

class Area2D extends Area{
float gridScale;
public Area2D(Poly p) {
super( (Polygon) p );
gridScale=p.gridScale;
}
public Area2D() {
public Area2D(float gScale) {
super();
gridScale=gScale;
}

boolean contains(int x, int y) {
double dx=x/gridScale;
double dy=y/gridScale;
return super.contains(dx,dy);
}

boolean contains(double x, double y) {
double dx=x/gridScale;
double dy=y/gridScale;
return super.contains(dx,dy);
}

void drawMe(){
PathIterator pathIter=getPathIterator(new AffineTransform());
beginShape();
float[] newCoords={0,0};
while(!pathIter.isDone()) {
pathIter.currentSegment(newCoords);
vertex(newCoords[0],newCoords[1]);
vertex(gridScale*newCoords[0],gridScale*newCoords[1]);
pathIter.next();
}
endShape();
Expand Down
8 changes: 5 additions & 3 deletions tests/AreaBooleans/AreaBooleans.pde
@@ -1,19 +1,21 @@
// Testing 2D boolean operation using Java built-in Area construct.
// - extending example from http://wiki.processing.org/w/Using_AWT's_Polygon_class
// - added gridScale parameter to handle int-only shapes like Polygons and general point-snapping.

Poly p1,p2;
Area2D a1,a2,a3;
float gridScale=0.01;

void setup() {
size(200,200);
int[]x={ 20,40,40,60,60,20 };
int[]y={ 20,20,40,40,60,60 };
p1=new Poly(x,y,6);
p2=new Poly(x,y,6);
p1=new Poly(gridScale,x,y,6);
p2=new Poly(gridScale,x,y,6);
p2.translate(20,20);
a1=new Area2D(p1);
a2=new Area2D(p2);
a3=new Area2D();
a3=new Area2D(gridScale);
a3.add(a1);
a3.intersect(a2);
}
Expand Down
50 changes: 46 additions & 4 deletions tests/AreaBooleans/Poly.pde
Expand Up @@ -8,15 +8,57 @@ import java.awt.Polygon;
*/

class Poly extends Polygon{
public Poly(int[] x,int[] y, int n){
//call the java.awt.Polygon constructor
super(x,y,n);
float gridScale;

public Poly(float gScale, int[] x,int[] y, int n) {
super();
gridScale=gScale;
//call the java.awt.Polygon addPoint function
for(int i=0;i<n;i++) {
super.addPoint((int) round(x[i]/gridScale),(int) round(y[i]/gridScale));
}
}

public Poly(float gScale, double[] x,double[] y, int n) {
super();
gridScale=gScale;
println("Area2D gridScale="+gridScale);
//call the java.awt.Polygon addPoint function
for(int i=0;i<n;i++) {
super.addPoint((int) (x[i]/gridScale),(int) (y[i]/gridScale));
}
}

void translate(int x, int y) {
super.translate((int) round(x/gridScale),(int) round(y/gridScale));
}

void translate(double x, double y) {
super.translate((int) (x/gridScale),(int) (y/gridScale));
}


boolean contains(int x, int y) {
double dx=x/gridScale;
double dy=y/gridScale;
return super.contains(dx,dy);
}

boolean contains(double x, double y) {
double dx=x/gridScale;
double dy=y/gridScale;
return super.contains(dx,dy);
}


float getGrid() {
return gridScale;
}

void drawMe(){
beginShape();
for(int i=0;i<npoints;i++){
vertex(xpoints[i],ypoints[i]);
vertex(round(gridScale*xpoints[i]),round(gridScale*ypoints[i]));
}
endShape();
}
Expand Down

0 comments on commit c30f63c

Please sign in to comment.