1创建类

  1. 简单类

类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;
    }
}
  1. 复杂类
    类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;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zttbee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值