PDF export problem..
Answered
- Need more info
- Answered
- Working on it
in
Core Library Questions
•
3 years ago
I‘m new to processing and prettz much lacking the time to explore it.., I only found the perfect code for what i need online and now need to export it, but it‘s just not working.. i tried this..
http://processing.org/reference/libraries/pdf/index.html but not working..
(a dxf would work too)
here‘s the code, as i found it on some website, only thing i modified is the image size:
import processing.pdf.*;
int NUMPOINTS;
int MAXORDER;
ArrayList voronois;
ArrayList borders;
ArrayList points;
import processing.pdf.*;
void setup(){
size(250,1500);
smooth();
NUMPOINTS=4; // Number of cells per iteration
MAXORDER=5; // Number of iteration. Total number of cells is NUMPOINTS^MAXORDER
borders=new ArrayList();
points=new ArrayList();
voronois=new ArrayList();
reset();
}
void reset(){
borders.clear();
points.clear();
voronois.clear();
generateBorders();
generatePoints(NUMPOINTS);
Voronoi mainVoronoi=new Voronoi(points,borders,0,color(150,150,150),color(0,120));
voronois.add(mainVoronoi);
divide(mainVoronoi);
loop();
}
void draw(){
drawVoronoi();
noLoop();// stop drawing, unless something changes
}
void drawVoronoi(){
background(0);
fill(255);
Iterator voronoiItr = voronois.iterator();
while(voronoiItr.hasNext()){
Voronoi currentVoronoi = (Voronoi)voronoiItr.next();
if(currentVoronoi.order>MAXORDER){
//stroke(0);
//fill(currentVoronoi.fillColor);
currentVoronoi.drawRounded(1f); //use drawRounded() for Bezier curves
/*fill(255);
currentVoronoi.drawRounded(.9f); */
}
}
/*voronoiItr = voronois.iterator();
while(voronoiItr.hasNext()){
Voronoi currentVoronoi = (Voronoi)voronoiItr.next();
if(currentVoronoi.order>MAXORDER){
stroke(currentVoronoi.strokeColor);
//strokeWeight(currentVoronoi.strokeWeight);
noFill();
currentVoronoi.drawRounded(.8f);
}
} */
}
Point2D map(Point2D p){
/*Point2D result=new Point2D();
result.x=p.x+random(-10f,10f);
result.y=p.y+random(-10f,10f);
return result;*/
return p;
}
void generateBorders(){// Create initial borders
borders.clear();
ArrayList borderPoints = new ArrayList();
Segment2D border=new Segment2D();
boolean circle=false;// If true, then the initial boundary is a circle
if (circle){
float a = TWO_PI/128.0f;
for(int i=0;i<128;i++){
Point2D borderPoint = new Point2D(width/2+width/2*cos(a*i),height/2+height/2*sin(a*i));
borderPoints.add(borderPoint);
}
for(int i=0;i<128;i++){
int nexti=(i==127)?0:i+1;
border=new Segment2D((Point2D)borderPoints.get(i),(Point2D)borderPoints.get(nexti),i);
borders.add(border);
}
}
else{
border=new Segment2D(0,0,width-1,0,0);
borders.add(border);
border=new Segment2D(0,height-1,0,0,1);
borders.add(border);
border=new Segment2D(width-1,0,width-1,height-1,2);
borders.add(border);
border=new Segment2D(width-1,height-1,0,height-1,3);
borders.add(border);
}
}
void generateBorders(ArrayList periphery){// Create borders from a cell (so a new graph can be made inside the cell)
borders.clear();
for(int i=0;i<periphery.size();i++){
Point2D pointi =(Point2D)periphery.get(i);
int nexti=i+1;
if(nexti>periphery.size()-1)nexti=0;
Point2D pointj=(Point2D)periphery.get(nexti);
borders.add(new Segment2D(pointi,pointj));
}
}
void generatePoints(int n){
points.clear();
float minx,maxx,miny,maxy;
minx=miny=20000000;
maxx=maxy=-1;
Iterator borderItr = borders.iterator();
while(borderItr.hasNext()){
Segment2D currentBorder =(Segment2D) borderItr.next();
minx=min(minx,currentBorder.start.x);
minx=min(minx,currentBorder.end.x);
miny=min(miny,currentBorder.start.y);
miny=min(miny,currentBorder.end.y);
maxx=max(maxx,currentBorder.start.x);
maxx=max(maxx,currentBorder.end.x);
maxy=max(maxy,currentBorder.start.y);
maxy=max(maxy,currentBorder.end.y);
}
for(int i=0;i<n;i++){
SegmentPoint2D trialPoint=new SegmentPoint2D(random(minx,maxx), random(miny,maxy));
while(!inside(trialPoint,borders)){
trialPoint=new SegmentPoint2D(random(minx,maxx), random(miny,maxy));
}
points.add(trialPoint);
}
}
void divide(final Voronoi voronoi){
if((voronoi.order<MAXORDER+1)){//Limits the recursion
Iterator cellItr = voronoi.cells.iterator();
while(cellItr.hasNext()){
Cell currentCell= (Cell) cellItr.next();
generateBorders(currentCell.periphery);
int dividedFillRed=(int)constrain(red(voronoi.fillColor)+random(-20,20),0,255);
int dividedFillGreen=(int)constrain(green(voronoi.fillColor)+random(-20,20),0,255);
int dividedFillBlue=(int)constrain(blue(voronoi.fillColor)+(int)random(-20,20),0,255);
int dividedFillAlpha=255;
int dividedStrokeRed=0;
int dividedStrokeGreen=0;
int dividedStrokeBlue=0;
int dividedStrokeAlpha=100;
int dividedNumpoints=(voronoi.order==MAXORDER)?1:NUMPOINTS;
generatePoints(dividedNumpoints);
Voronoi divVoronoi=new Voronoi(points,borders,voronoi.order+1, color(dividedFillRed,dividedFillGreen,dividedFillBlue,dividedFillAlpha),color(dividedStrokeRed,dividedStrokeGreen,dividedStrokeBlue,dividedStrokeAlpha));
voronois.add(divVoronoi);
divide(divVoronoi);
}
}
}
void mousePressed(){
reset();
}
void keyPressed(){
if((key=='s')||(key=='S')){
int randomID = (int)random(10000000);
beginRecord(PDF, "voronoi"+randomID+".pdf");
drawVoronoi();
endRecord();
}
}
class Voronoi{
int NUMPOINTS;
int order;
ArrayList initialPoints;
ArrayList initialSegments;
ArrayList bisectors;
ArrayList borders;
ArrayList bisectorIntersections;
ArrayList cells;
color fillColor;
color strokeColor;
Voronoi(final ArrayList points, final ArrayList borders, final int o, final color fc,final color sc){
order=o;
NUMPOINTS=points.size();
initializeLists();
this.borders=borders;
initialPoints=points;
generateInitialSegments();
generateBisectors();
generateBisectorIntersections();
generateVoronoiSegments();
fillColor=fc;
strokeColor=sc;
clearConstruction();
}
void draw(float sc){
Iterator cellItr =cells.iterator();
while(cellItr.hasNext()){
((Cell)cellItr.next()).draw(sc);
}
}
void draw(int steps,float sc){
Iterator cellItr =cells.iterator();
while(cellItr.hasNext()){
((Cell)cellItr.next()).draw(steps,sc);
}
}
void drawRounded(float sc){
Iterator cellItr =cells.iterator();
while(cellItr.hasNext()){
((Cell)cellItr.next()).drawRounded(sc);
}
}
void initializeLists(){
initialPoints = new ArrayList();
initialSegments = new ArrayList();
bisectors = new ArrayList();
bisectorIntersections = new ArrayList();
cells = new ArrayList();
}
void clearConstruction(){
initialSegments.clear();
bisectors.clear();
bisectorIntersections.clear();
}
void generateInitialSegments(){
for(int i=0;i<NUMPOINTS;i++){
SegmentPoint2D pointi=(SegmentPoint2D)initialPoints.get(i);
for(int j=i+1;j<NUMPOINTS;j++){
SegmentPoint2D pointj=(SegmentPoint2D)initialPoints.get(j);
Segment2D segmentij = new Segment2D(pointi.convertToPoint2D(),pointj.convertToPoint2D());
initialSegments.add(segmentij);
pointi.add(segmentij);
pointj.add(segmentij);
}
}
}
void generateBisectors(){
Iterator itr = initialSegments.iterator();
while (itr.hasNext( )) {
Segment2D currentSegment=(Segment2D)itr.next();
Point2D midPoint = new Point2D(0.5f*currentSegment.start.x+0.5f*currentSegment.end.x, 0.5f*currentSegment.start.y+0.5f*currentSegment.end.y);
Point2D relPoint =new Point2D(currentSegment.start.y-currentSegment.end.y, currentSegment.end.x-currentSegment.start.x);
Point2D startPoint=new Point2D(midPoint.x-relPoint.x,midPoint.y-relPoint.y);
Point2D endPoint=new Point2D(midPoint.x+relPoint.x,midPoint.y+relPoint.y);
Segment2D bisector = new Segment2D(extendToBorder(new Segment2D(startPoint,endPoint), borders));
bisectors.add(bisector);
currentSegment.bisector = bisector;
}
}
void generateBisectorIntersections(){
for(int i=0;i<bisectors.size();i++){
Segment2D bisectori = (Segment2D)bisectors.get(i);
for(int j=i+1;j<bisectors.size();j++){
Segment2D bisectorj = (Segment2D)bisectors.get(j);
Point2D result = segmentIntersectionWithSegment(bisectori, bisectorj);
if (result!=null) {
bisectorIntersections.add(result);
bisectori.points.add(result);
bisectorj.points.add(result);
}
}
}
}
void generateVoronoiSegments(){
Iterator centerPointItr = initialPoints.iterator();
while(centerPointItr.hasNext()){
SegmentPoint2D currentCenterPoint = (SegmentPoint2D)centerPointItr.next();
ArrayList periphery = new ArrayList();
Iterator segmentItr = currentCenterPoint.belongsToSegment.iterator();
while(segmentItr.hasNext()){
Segment2D currentSegment = (Segment2D)segmentItr.next();
Segment2D currentBisector = currentSegment.bisector;
Iterator sweepPointItr = currentBisector.points.iterator();
while(sweepPointItr.hasNext()){
Point2D currentSweepPoint = (Point2D)sweepPointItr.next();
Segment2D currentSweepRay = new Segment2D(currentCenterPoint,currentSweepPoint);
Iterator sweepSegmentItr = currentCenterPoint.belongsToSegment.iterator();
boolean intersect = false;
while(sweepSegmentItr.hasNext()){
Segment2D currentSweepSegment =(Segment2D)sweepSegmentItr.next();
Segment2D currentSweepBisector=currentSweepSegment.bisector;
Point2D sweepIntersection = segmentIntersectionWithSegment(currentSweepRay, currentSweepBisector);
if ((sweepIntersection!=null)&&(dist(sweepIntersection,currentSweepPoint)>0.1f)){
intersect = true;
break;
}
}
if(!intersect){
Point2D peripheryPoint = new Point2D(currentSweepPoint);
periphery.add(peripheryPoint);
}
}
}
segmentItr = borders.iterator();
while(segmentItr.hasNext()){
Segment2D currentSegment = (Segment2D)segmentItr.next();
Iterator sweepPointItr = currentSegment.points.iterator();
while(sweepPointItr.hasNext()){
Point2D currentSweepPoint = (Point2D)sweepPointItr.next();
Segment2D currentSweepRay = new Segment2D(currentCenterPoint,currentSweepPoint);
Iterator sweepSegmentItr = currentCenterPoint.belongsToSegment.iterator();
boolean intersect = false;
while(sweepSegmentItr.hasNext()){
Segment2D currentSweepSegment =(Segment2D)sweepSegmentItr.next();
Segment2D currentSweepBisector=currentSweepSegment.bisector;
Point2D sweepIntersection = segmentIntersectionWithSegment(currentSweepRay, currentSweepBisector);
if ((sweepIntersection!=null)&&(dist(sweepIntersection,currentSweepPoint)>0.1f)){
intersect = true;
break;
}
}
if(!intersect){
Point2D peripheryPoint = new Point2D(currentSweepPoint);
periphery.add(peripheryPoint);
}
}
}
Cell cell = new Cell(currentCenterPoint.convertToPoint2D());
cell.periphery.addAll(periphery);
cell.update();
cells.add(cell);
}
}
}
class Point2D{
float x,y;
int onBorder;
Point2D(){
x=y=0.0f;
onBorder=-1;
}
Point2D(final float x, final float y){
this.x=x;
this.y=y;
onBorder=-1;
}
Point2D(final float x, final float y, final int id){
this.x=x;
this.y=y;
onBorder=id;
}
Point2D(final Point2D p){
x=p.x;
y=p.y;
onBorder=p.onBorder;
}
boolean equals(final Point2D point){
return((point.x==x)&&(point.y==y));
}
boolean equals(final Point2D point, float threshold){
return((point.x<x+threshold)&&(point.x>x-threshold)&&(point.y<y+threshold)&&(point.y>y-threshold));
}
void draw(){
ellipse(x,y,4,4);
}
}
class SegmentPoint2D extends Point2D{
ArrayList belongsToSegment;
SegmentPoint2D(){
super();
belongsToSegment = new ArrayList();
}
SegmentPoint2D(final float x, final float y){
super(x,y);
belongsToSegment = new ArrayList();
}
SegmentPoint2D(final float x, final float y, final int id){
super(x,y,id);
belongsToSegment = new ArrayList();
}
SegmentPoint2D(final Point2D p){
super(p);
belongsToSegment = new ArrayList();
}
void add(final Segment2D segment){
belongsToSegment.add(segment);
}
Point2D convertToPoint2D(){
return new Point2D(x,y,onBorder);
}
}
class PeripheryPoint2D extends Point2D{
float angle;
Point2D centerPoint;
PeripheryPoint2D(final Point2D point, final Point2D centerPoint){
super(point);
this.centerPoint=new Point2D(centerPoint);
angle=atan2(point.y-centerPoint.y,point.x-centerPoint.x);
}
PeripheryPoint2D(final PeripheryPoint2D point){
super(point);
centerPoint=new Point2D(point.centerPoint);
angle=point.angle;
}
Point2D convertToPoint2D(){
return new Point2D(x,y,onBorder);
}
}
class Segment2D{
Point2D start;
Point2D end;
ArrayList points;
Segment2D bisector=null;
Segment2D(){
start = new Point2D(0.0f,0.0f);
end = new Point2D(0.0f,0.0f);
points=new ArrayList();
points.add(start);
points.add(end);
}
Segment2D(final float startx,final float starty,final float endx,final float endy){
start = new Point2D(startx,starty);
end = new Point2D(endx,endy);
points=new ArrayList();
points.add(start);
points.add(end);
}
Segment2D(final float startx,final float starty,final float endx,final float endy, int id){
start = new Point2D(startx,starty);
end = new Point2D(endx,endy);
start.onBorder=id;
end.onBorder=id;
points=new ArrayList();
points.add(start);
points.add(end);
}
Segment2D(final Point2D start,final Point2D end){
this.start = new Point2D(start);
this.end = new Point2D(end);
points=new ArrayList();
points.add(start);
points.add(end);
}
Segment2D(final Point2D start,final Point2D end, int id){
this.start = new Point2D(start);
this.end = new Point2D(end);
this.start.onBorder=id;
this.end.onBorder=id;
points=new ArrayList();
points.add(start);
points.add(end);
}
Segment2D(final Segment2D segment){
start = new Point2D(segment.start);
end = new Point2D(segment.end);
points=new ArrayList();
points.add(start);
points.add(end);
}
void add(Point2D point){
Iterator itr = points.iterator();
boolean unique=true;
while (itr.hasNext()) {
if (point.equals((Point2D)itr.next(),0.1f)){
unique=false;
break;
}
}
if (unique) points.add(point);
}
Point2D get(final int i){
return (Point2D)points.get(i);
}
void draw(){
line(start.x,start.y,end.x,end.y);
}
}
class Cell{
Point2D centerPoint;
ArrayList periphery;
Cell(final Point2D point){
centerPoint=new Point2D(point);
periphery = new ArrayList();
}
void update(){
clean();
sort();
removeCollinearPoints();
}
void clean(){
ArrayList cleanedPeriphery = new ArrayList();
for(int i=0;i<periphery.size();i++){
Point2D pointi=(Point2D)periphery.get(i);
boolean unique=true;
for(int j=i+1;j<periphery.size();j++){
Point2D pointj=(Point2D)periphery.get(j);
if (pointi.equals(pointj,0.1f)){
unique=false;
break;
}
}
if(unique) cleanedPeriphery.add(pointi);
}
periphery.clear();
periphery.addAll(cleanedPeriphery);
}
void sort(){
for (int i = periphery.size(); --i >= 0; ) {
for (int j = 0; j < i; j++) {
PeripheryPoint2D pointj = new PeripheryPoint2D((Point2D)periphery.get(j),centerPoint);
PeripheryPoint2D pointjj = new PeripheryPoint2D((Point2D)periphery.get(j+1),centerPoint);
if (pointj.angle > pointjj.angle) {
PeripheryPoint2D temp = new PeripheryPoint2D(pointj);
periphery.set(j,pointjj.convertToPoint2D());
periphery.set(j+1,temp.convertToPoint2D());
}
}
}
}
void removeCollinearPoints(){
ArrayList simplePeriphery = new ArrayList();
for(int i=0;i<periphery.size();i++){
Point2D pointi=(Point2D)periphery.get(i);
int j = i-1;
if(j<0) j=periphery.size()-1;
Point2D pointj=(Point2D)periphery.get(j);
int k = i+1;
if(k==periphery.size()) k=0;
Point2D pointk=(Point2D)periphery.get(k);
if (!colinear(pointi,pointj,pointk,0.01f)){
simplePeriphery.add(pointi);
}
}
periphery.clear();
periphery.addAll(simplePeriphery);
}
void draw(float sc){
int s=periphery.size();
pushMatrix();
float cogx=0f;
float cogy=0f;
for(int i=0;i<s;i++){
Point2D currentPoint= (Point2D)periphery.get(i);
cogx+=currentPoint.x;
cogy+=currentPoint.y;
}
cogx/=s;
cogy/=s;
translate(cogx,cogy);
scale(sc);
translate(-cogx,-cogy);
beginShape();
Iterator itr =periphery.iterator();
while(itr.hasNext()){
Point2D currentPoint= map((Point2D)itr.next());
vertex(currentPoint.x,currentPoint.y);
}
endShape(CLOSE);
popMatrix();
}
void drawRounded(float sc){
int s=periphery.size();
float scf=1.0f;
if(s>2){
pushMatrix();
float cogx=0f;
float cogy=0f;
for(int i=0;i<s;i++){
Point2D currentPoint= (Point2D)periphery.get(i);
cogx+=currentPoint.x;
cogy+=currentPoint.y;
}
cogx/=s;
cogy/=s;
translate(cogx,cogy);
scale(sc);
translate(-cogx,-cogy);
beginShape();
Point2D currentPoint= map((Point2D)periphery.get(0));
Point2D nextPoint= map((Point2D)periphery.get(1));
Point2D previousPoint= map((Point2D)periphery.get(s-1));
vertex(0.5f*currentPoint.x+0.5f*previousPoint.x,0.5f*currentPoint.y+0.5f*previousPoint.y);
bezierVertex(scf*currentPoint.x+(1f-scf)*previousPoint.x,scf*currentPoint.y+(1f-scf)*previousPoint.y,scf*currentPoint.x+(1f-scf)*nextPoint.x,scf*currentPoint.y+(1f-scf)*nextPoint.y,0.5f*currentPoint.x+0.5f*nextPoint.x,0.5f*currentPoint.y+0.5f*nextPoint.y);
for(int i=1;i<s-1;i++){
currentPoint= map((Point2D)periphery.get(i));
nextPoint= map((Point2D)periphery.get(i+1));
previousPoint= map((Point2D)periphery.get(i-1));
bezierVertex(scf*currentPoint.x+(1f-scf)*previousPoint.x,scf*currentPoint.y+(1f-scf)*previousPoint.y,scf*currentPoint.x+(1f-scf)*nextPoint.x,scf*currentPoint.y+(1f-scf)*nextPoint.y,0.5f*currentPoint.x+0.5f*nextPoint.x,0.5f*currentPoint.y+0.5f*nextPoint.y);
}
currentPoint= map((Point2D)periphery.get(s-1));
nextPoint= map((Point2D)periphery.get(0));
previousPoint= map((Point2D)periphery.get(s-2));
bezierVertex(scf*currentPoint.x+(1f-scf)*previousPoint.x,scf*currentPoint.y+(1f-scf)*previousPoint.y,scf*currentPoint.x+(1f-scf)*nextPoint.x,scf*currentPoint.y+(1f-scf)*nextPoint.y,0.5f*currentPoint.x+0.5f*nextPoint.x,0.5f*currentPoint.y+0.5f*nextPoint.y);
endShape(CLOSE);
popMatrix();
}
}
void draw(int steps, float sc){
int locsteps=max(1,steps);
int s=periphery.size();
pushMatrix();
float cogx=0f;
float cogy=0f;
for(int i=0;i<s;i++){
Point2D currentPoint= (Point2D)periphery.get(i);
cogx+=currentPoint.x;
cogy+=currentPoint.y;
}
cogx/=s;
cogy/=s;
translate(cogx,cogy);
scale(sc);
translate(-cogx,-cogy);
beginShape();
for(int i=0;i<periphery.size();i++){
Point2D currentPoint=(Point2D)periphery.get(i);
int ii=(i==periphery.size()-1)?0:i+1;
Point2D nextPoint= (Point2D)periphery.get(ii);
for(int j=0;j<locsteps;j++){
float f=j/(float)locsteps;
Point2D intPoint= map(new Point2D(currentPoint.x+f*(nextPoint.x-currentPoint.x),currentPoint.y+f*(nextPoint.y-currentPoint.y)));
vertex(intPoint.x,intPoint.y);
}
}
endShape(CLOSE);
popMatrix();
}
}
float dist(Point2D point1,Point2D point2){
return dist(point1.x, point1.y,point2.x,point2.y);
}
Point2D segmentIntersectionWithSegment(final Segment2D segment1, final Segment2D segment2){
Point2D result=null;
float denominator = (segment2.end.y-segment2.start.y)*(segment1.end.x-segment1.start.x)-(segment2.end.x-segment2.start.x)*(segment1.end.y-segment1.start.y);
if (denominator==0.0f) return result;
float ua=((segment2.end.x-segment2.start.x)*(segment1.start.y-segment2.start.y)-(segment2.end.y-segment2.start.y)*(segment1.start.x-segment2.start.x))/denominator;
float ub=((segment1.end.x-segment1.start.x)*(segment1.start.y-segment2.start.y)-(segment1.end.y-segment1.start.y)*(segment1.start.x-segment2.start.x))/denominator;
if((ua>1.0f)||(ua<0.0f)||(ub>1.0f)||(ub<0.0f)) return result;
result=new Point2D(segment1.start.x+ua*(segment1.end.x-segment1.start.x),segment1.start.y+ua*(segment1.end.y-segment1.start.y));
return result;
}
Point2D segmentIntersectionWithLine(final Segment2D segment1, final Segment2D segment2){
Point2D result=null;
float denominator = (segment2.end.y-segment2.start.y)*(segment1.end.x-segment1.start.x)-(segment2.end.x-segment2.start.x)*(segment1.end.y-segment1.start.y);
if (denominator==0.0f) return result;
float ua=((segment2.end.x-segment2.start.x)*(segment1.start.y-segment2.start.y)-(segment2.end.y-segment2.start.y)*(segment1.start.x-segment2.start.x))/denominator;
if((ua>1.0f)||(ua<0.0f)) return result;
result=new Point2D(segment1.start.x+ua*(segment1.end.x-segment1.start.x),segment1.start.y+ua*(segment1.end.y-segment1.start.y));
return result;
}
boolean colinear(final Point2D p1,final Point2D p2,final Point2D p3,final float threshold){
//Three points are considered collinear if p1 lies very close to the line p2-p3.
float d2=(p3.x-p2.x)*(p3.x-p2.x)+(p3.y-p2.y)*(p3.y-p2.y);
if(d2<threshold*threshold) return true;
float u=((p1.x-p2.x)*(p3.x-p2.x)+(p1.y-p2.y)*(p3.y-p2.y))/d2;
float x=p2.x+u*(p3.x-p2.x);
float y=p2.y+u*(p3.y-p2.y);
return ((p1.x-x)*(p1.x-x)+(p1.y-y)*(p1.y-y))<threshold*threshold;
}
boolean inside(final Point2D point, final ArrayList borders){
boolean result=false;
Segment2D testSegment = new Segment2D(point.x,point.y,width+1,point.y);
int numIntersections=0;
Iterator borderItr = borders.iterator();
while(borderItr.hasNext()){
Segment2D currentBorder =(Segment2D) borderItr.next();
if(segmentIntersectionWithSegment(testSegment,currentBorder)!=null) numIntersections++;
}
if((numIntersections%2==1)) result=true;
numIntersections=0;
testSegment = new Segment2D(point.x,point.y,point.x,height+1);
borderItr = borders.iterator();
while(borderItr.hasNext()){
Segment2D currentBorder =(Segment2D) borderItr.next();
if(segmentIntersectionWithSegment(testSegment,currentBorder)!=null) numIntersections++;
}
if((numIntersections%2==1)) result=true;
return result;
}
Segment2D extendToBorder(final Segment2D segment, final ArrayList borders){
Point2D startPoint = null ;
Iterator itr = borders.iterator();
int border=-1;
Segment2D currentBorder=new Segment2D();
while ((itr.hasNext( ))&&(startPoint == null)){
currentBorder=(Segment2D)itr.next();
startPoint = segmentIntersectionWithLine(currentBorder,segment);
border++;
}
if(startPoint != null){
startPoint.onBorder=border;
currentBorder.add(startPoint);
}
Point2D endPoint = null ;
while ((itr.hasNext( ))&&(endPoint == null)){
currentBorder=(Segment2D)itr.next();
endPoint = segmentIntersectionWithLine(currentBorder,segment);
border++;
}
if(endPoint != null){
endPoint.onBorder=border;
currentBorder.add(endPoint);
}
if((startPoint != null)&&(endPoint != null)) return new Segment2D(startPoint,endPoint);
return new Segment2D(segment);
}
1