applet - Java Scanline Polygon Fill Algorithm -
i have started learning computer graphics , new java. java applet performs scan line algorithm works cases. can me out know wrong ?
you can view entire code here = http://codeshare.io/9uvuf in advance.
i've made similar in c++ , opengl. work concave , convex, not sure if polygons.
i don't know if it's best solution, works me.
bool graphicobject::ismouseinside(int x, int y) { if (points.size() < 2) { return false; } int count = 0; float ti; float yint = y; float xint = 0; (size_t = 0; < points.size() - 1; i++) { auto p1 = points[i + 1]; auto p2 = points[i]; if (p1.x == x && p1.y == y) { return true; } ti = 0; if ((p2.y - p1.y) != 0) { ti = (yint - p1.y) / (p2.y - p1.y); } if (ti > 0 && ti < 1) { xint = p1.x + (p2.x - p1.x) * ti; // hack: point (line) impossible select. // add error margin. if (points.size() == 2) { // todo: use euclidean distance this. better? if ((xint + 8) > x || (xint - 8) > x) { count++; } } else { if (xint > x) { count++; } } } } // last point first. auto p1 = points[points.size() - 1]; auto p2 = points[0]; ti = (yint - p1.y) / (p2.y - p1.y); if (ti > 0 && ti < 1) { xint = p1.x + (p2.x - p1.x) * ti; if (xint > x) { count++; } } return count % 2 != 0; }
Comments
Post a Comment