| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 545 人关注过本帖
标题:我用java编的多边形裁剪程序,但是有时候能运行,有时候不能运行,大虾帮看 ...
只看楼主 加入收藏
Dhuyang
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-10-12
收藏
 问题点数:0 回复次数:0 
我用java编的多边形裁剪程序,但是有时候能运行,有时候不能运行,大虾帮看下
这是线段裁剪的类,我不知道哪里出问题了……
这里我采用了Point intersection(Point p1, Point p2, Point p3, Point p4) 这个函数来进行输入线段和多边形每条线段之间的求交点,当没有交点的时候就返回(0,0),各位大虾快帮我看下阿,想了好几天了,压抑阿
程序代码:
class cutall extends JFrame {
    JLabel title1, title2, title3, title4;
    JTextField t1, t2, t3, t4, t5;
    JPanel pane, pan, pa,pb;
    int width = getToolkit().getScreenSize().width, minx = -width / 2,
            maxx = width / 2;
    int height = getToolkit().getScreenSize().height, miny = -height / 2,
            maxy = height / 2;
    JButton butt;
    int rows, rotate[] = new int[1];// rows表示点数
    JTable table;
    Object a[][];
    Object name[] = { "横坐标", "纵坐标" };

    cutall() {
        pane = new JPanel();
        pan = new JPanel();
        pa = new JPanel();
        pb =new JPanel();
        pane.setLayout(new GridLayout(5, 1));
        title1 = new JLabel("范围(" + minx + "," + miny
                + ")到(" + maxx + "," + maxy + ")");
        title2 = new JLabel(
                "                                      请按照顺时针或者逆时针输入多边形所有点");
        title4 = new JLabel("请输入线段的两个端点坐标:");
        title3 = new JLabel("输入截线段多边形点的个数,回车确认        ");
        butt = new JButton("设置完成");
        t1 = new JTextField(10);
        t2 = new JTextField(5);
        t3 = new JTextField(5);
        t4 = new JTextField(5);
        t5 = new JTextField(5);
        pa.add(title4);
        pa.add(t2);
        pa.add(t3);
        pb.add(title1);
        pb.add(t4);
        pb.add(t5);
        
        pane.add(pa);
        
        pane.add(pb);
        
        
        pan.add(title3);
        pan.add(t1);
        t1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                rows = Integer.parseInt(t1.getText());
                a = new Object[rows][2];
                for (int i = 0; i < rows; i++) {
                    for (int j = 0; j < 2; j++)
                        a[i][j] = 0;
                }
                table = new JTable(a, name);
                table.setRowHeight(20);
                getContentPane().removeAll();
                add(pane, BorderLayout.NORTH);
                add(butt, BorderLayout.SOUTH);
                add(new JScrollPane(table), BorderLayout.CENTER);
                validate();
            }
        });
        butt.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Point p1 = new Point(Integer.parseInt(t2.getText()), Integer
                        .parseInt(t3.getText()));
                Point p2 = new Point(Integer.parseInt(t4.getText()), Integer
                        .parseInt(t5.getText()));
                new cut(a, p1, p2);
            }
        });
        pane.add(pan);
        pane.add(title2);
        add(pane, BorderLayout.NORTH);
        add(butt, BorderLayout.SOUTH);
        add(new JScrollPane(table), BorderLayout.CENTER);
        setBounds(440, 250, 400, 300);
        setTitle("线段裁剪的基本设置");
        setVisible(true);
        setResizable(false);
    }
}

class cut extends JFrame {
    Object a[][];
    Point p1, p2;
    int i, j, x, y;
    int width = getToolkit().getScreenSize().width, minx = -width / 2,
            maxx = width / 2;
    int height = getToolkit().getScreenSize().height, miny = -height / 2,
            maxy = height / 2;

    public cut(Object a[][], Point p1, Point p2) {
        this.p1 = p1;
        this.p2 = p2;
        this.a = a;
        add(new cutpolygo());
        setTitle("线段的裁剪设置");
        setLocation(0, 0);
        setSize(width, height);
        setVisible(true);
        setVisible(true);
        setVisible(true);
        setResizable(false);
    }

    class cutpolygo extends JPanel {
        public void paintComponent(Graphics g) {
            int j = 0, c, d, len[] = new int[20];
            Point pp[] = new Point[20];
            Point p3, p4, p0 = new Point(0, 0), p5 = new Point(0, 0);
            Graphics2D t = (Graphics2D) g;
            Polygon p = new Polygon();
            p1.x=p1.x+maxx;
            p1.y=maxy-p1.y;
            p2.x=p2.x+maxx;
            p2.y=maxy-p2.y;
            for (int i = 0; i < a.length; i++) {
                x = Integer.parseInt(a[i][0].toString()) + maxx;
                y = -Integer.parseInt(a[i][1].toString()) + maxy;
                p.addPoint(x, y);
                p3 = new Point(x, y);
                c = Integer.parseInt(a[(i + 1) % a.length][0].toString())+ maxx;
                d = -Integer.parseInt(a[(i + 1) % a.length][1].toString())+ maxy;
                p4 = new Point(c, d);
                pp[j] = intersection(p1, p2, p3, p4);
                if (pp[j] != p0)
                    j++;
            }
            t.drawPolygon(p);
            t.setColor(Color.red);
            t.drawLine(p1.x, p1.y, p2.x, p2.y);
            for (int s = 0; s < j; s++)
                len[s] = (p1.x - pp[s].x) * (p1.x - pp[s].x) + (p1.y - pp[s].y)
                        * (p1.y - pp[s].y);
            for (int n = 0; n < j - 1; n++) {
                for (int m = 0; m < j - n - 1; m++) {
                    if (len[m] > len[m + 1]) {
                        p5 = pp[m];
                        pp[m] = pp[m + 1];
                        pp[m + 1] = p5;
                    }
                }
            }
                for (i = 0; i < j; i = i + 2) {
                    t.setColor(Color.green);
                    t.drawLine(p1.x, p1.y, pp[i].x, pp[i].y);
                }
                t.setColor(Color.green);
                t.drawLine(p2.x, p2.y, pp[i-1].x, pp[i-1].y);
        }
    }

    Point intersection(Point p1, Point p2, Point p3, Point p4) {
        Point p = new Point(0, 0);
        float x1 = p1.x, x2 = p2.x, x3 = p3.x, x4 = p4.x;
        float y1 = p1.y, y2 = p2.y, y3 = p3.y, y4 = p4.y;
        float d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
        if (d == 0)
            return p;
        float pre = (x1 * y2 - y1 * x2), post = (x3 * y4 - y3 * x4);
        float x = (pre * (x3 - x4) - (x1 - x2) * post) / d;
        float y = (pre * (y3 - y4) - (y1 - y2) * post) / d;
        if (x < Math.min(x1, x2) || x > Math.max(x1, x2)
                || x < Math.min(x3, x4) || x > Math.max(x3, x4))
            return p;
        if (y < Math.min(y1, y2) || y > Math.max(y1, y2)
                || y < Math.min(y3, y4) || y > Math.max(y3, y4))
            return p;
        Point ret = new Point();
        ret.x = (int) x;
        ret.y = (int) y;
        return ret;
    }
}
搜索更多相关主题的帖子: 裁剪 
2011-10-12 10:47
快速回复:我用java编的多边形裁剪程序,但是有时候能运行,有时候不能运行,大虾 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.048534 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved