Wednesday, 15 January 2014

java - Adding Polygon points from Line2D values -



java - Adding Polygon points from Line2D values -

i have little block of code reading line2d values array called linelist (in different class) , storing them in new array called list. here have been trying convert of line values polygon points (a point each x, y coordinate of line end).

so far have working not working first point of first line in array (that's suspect is) added , having problem finding solution have tried including in first if statement.

i appreciate help able provide me on this.

below code using adding points line2d values:

polygon p = new polygon(); arraylist<line2d> list = new arraylist<line2d>(); color pixel; boolean firsttime = true; list = segmentation.getlinelist(); //loop through linelist , add together x , y coordinates relative x , y arrays for(int = 0; < list.size(); i++) { if(firsttime == true){ line2d line = list.get(i); point2d startpoint = line.getp1(); point2d endpoint = line.getp2(); int startx = (int) startpoint.getx(); int starty = (int) startpoint.gety(); int endx = (int) endpoint.getx(); int endy = (int) endpoint.gety(); p.addpoint(p.xpoints[i] = startx, p.ypoints[i] = starty); p.addpoint(p.xpoints[i] = endx, p.ypoints[i] = endy); startpoint = null; endpoint = null; line = null; firsttime = false; } else { line2d line = list.get(i); point2d endpoint = line.getp2(); int endx = (int) endpoint.getx(); int endy = (int) endpoint.gety(); p.addpoint(p.xpoints[i] = endx, p.ypoints[i] = endy); endpoint = null; line = null; } }

below illustration of first point (lower point) not beingness included in polygon points.

seems lot of duplicated code me. before seek more debugging, let's refactor code , create simpler understand , debug.

refactoring

the first bit of code can pull out code add together point polygon. here's new method.

protected void addpoint(polygon p, point2d point) { int x = (int) point.getx(); int y = (int) point.gety(); p.addpoint(x, y); }

now, didn't in 1 refactoring. first pulled out end point code, because identical. after reflecting on code more, generalized utilize start point code.

when first saw line of code

p.addpoint(p.xpoints[i] = startx, p.ypoints[i] = starty);

i thought, wtf? i'd never seen set values in method call. in clause, sure.

after 5 minutes of thought, realized polygon class internal values beingness set after execution of addpoint method. while might useful other method call, it's not necessary here. method phone call can simplified to

p.addpoint(x, y);

java developers, if need yet reason making class variables non-public, real one. keeps people setting class variables after you've set them in setter method.

priming read

we can rid of first time switch, , lot of code, if utilize little known algorithm called priming read.

most loops have input statement first statement in loop. for (string s : stringlist) construction of loop hides fact input statement first statement in loop.

but sometimes, have method need priming read. method 1 of times.

in pseudo code, priming read works this.

read input loop process input read input end loop process lastly input

by using priming read, able simplify createpolygon method.

any cobol programmer reading thought, "yep, priming read."

java programmers, maintain priming read thought in mind. won't utilize often, see, reduces amount of code need in cases.

refactored code

public polygon createpolygon(segmentation segmentation) { polygon p = new polygon(); list<line2d> list = segmentation.getlinelist(); if (list.size() < 2) homecoming p; line2d line = list.get(0); addpoint(p, line.getp1()); // loop through linelist , add together x , y coordinates relative x // , y arrays (int = 1; < list.size(); i++) { addpoint(p, line.getp2()); line = list.get(i); } addpoint(p, line.getp2()); homecoming p; } protected void addpoint(polygon p, point2d point) { int x = (int) point.getx(); int y = (int) point.gety(); p.addpoint(x, y); }

i did 2 additional things code.

i added test less 2 lines. basically, takes @ to the lowest degree 2 lines create triangle (polygon). there no point executing method 1 line or 0 lines.

i changed arraylist reference list. in java, it's improve utilize interface on concrete class. since list method we're using in code method, can utilize interface. advantage using interface createpolygon method doesn't care whether or not getlinelist method returns arraylist, linkedlist, or custom class implements list. makes future modifications easier.

java awt polygon java-2d point-in-polygon

No comments:

Post a Comment