- 简单类
类EdgeY:有四个成员变量(xi,dx,ym,next),其中next结构与EdgeY相同;有一个构造方法。
class EdgeY {
//成员变量
double xi;
double dx;
int ym;
EdgeY next = null;
//构造方法,名字与类名相同
EdgeY(double xi, double dx, int ym) {
this.xi = xi;//属性xi=形参xi;
this.dx = dx;
this.ym = ym;
}
}
- 复杂类
类NETY:有三个成员变量(数组bucket,ymin,ymax);有四个成员方法。
class NETY {//new NET
EdgeY[] bucket;// the array of edge
int ymin, ymax;//min and max
void create() {
createNETY();// create
}
void getMinMax() {//get min
ymin = polygon[0].y;//first is the first
ymax = polygon[0].y;
for (int i = 1; i < polygon.length; i++) {
if (polygon[i].y > ymax) {//get the max
ymax = polygon[i].y;
}
if (polygon[i].y < ymin) {//get the min
ymin = polygon[i].y;
}
}
}
void createNETY() {
bucket = new EdgeY[ymax - ymin + 1];//the array of thr edge
int n = polygon.length;//n is the length
for (int y = ymin; y <= ymax; y++) {
bucket[y - ymin] = new EdgeY(0, 0, y);//to give value
EdgeY tmp = bucket[y - ymin];// give the net to tmp
for (int cur = 0; cur < n; cur++) {//for each point
if (y == polygon[cur].y) {//
int front = (cur - 1 + n) % n;// the front
int rear = (cur + 1) % n;// the rear
EdgeY firstEdge = null, secondEdge = null;//first edge ,second edge
double firstDx = -9999;//first dx
double secondDx = -9999;//second dx
if (polygon[front].y > y) {//if the front y is up y
// 1/dx
firstDx = (polygon[cur].x - polygon[front].x) * 1.0
/ (polygon[cur].y - polygon[front].y);
firstEdge = new EdgeY(polygon[cur].x, firstDx,
polygon[front].y);
tmp.next = firstEdge;//next is first
tmp = firstEdge;//now is the first
}
if (polygon[rear].y > y) {//if the rear y is up than y
//1/dx
secondDx = (polygon[cur].x - polygon[rear].x) * 1.0
/ (polygon[cur].y - polygon[rear].y);
secondEdge = new EdgeY(polygon[cur].x, secondDx,
polygon[rear].y);
tmp.next = secondEdge;
tmp = secondEdge;
}
}
}
if(inside.size() != 0)
{
for(int i=0;i<inside.size();i++)
{
Point[] in=inside.get(i);
int m = in.length;
for (int cur = 0; cur < m; cur++) {
if (y == in[cur].y) {
int front = (cur - 1 + m) % m;// front
int rear = (cur + 1) % m;// rear
EdgeY firstEdge = null, secondEdge = null;
double firstDx = -9999;
double secondDx = -9999;
if (in[front].y > y) {
// first dx
firstDx = (in[cur].x - in[front].x) * 1.0
/ (in[cur].y - in[front].y);
firstEdge = new EdgeY(in[cur].x, firstDx,
in[front].y);
tmp.next = firstEdge;
tmp = firstEdge;
}
//dx
if (in[rear].y > y) {
// ����߽�㲢��ʼ��
secondDx = (in[cur].x - in[rear].x) * 1.0
/ (in[cur].y - in[rear].y);
secondEdge = new EdgeY(in[cur].x, secondDx,
in[rear].y);
tmp.next = secondEdge;
tmp = secondEdge;
}
}
}
}
}
/**************************************/
}
}
//exp
void Traverse() {
for (int i = 0; i < bucket.length; i++) {
EdgeY tmp = bucket[i];
//System.out.println("1" + tmp.ym);
while (tmp.next != null) {
System.out.println(tmp.next.dx + "");
tmp = tmp.next;
}
}
}
}