Java实现最优三角剖分算法并图形显示四

上传人:飞*** 文档编号:47446443 上传时间:2018-07-02 格式:PDF 页数:10 大小:6.30KB
返回 下载 相关 举报
Java实现最优三角剖分算法并图形显示四_第1页
第1页 / 共10页
Java实现最优三角剖分算法并图形显示四_第2页
第2页 / 共10页
Java实现最优三角剖分算法并图形显示四_第3页
第3页 / 共10页
Java实现最优三角剖分算法并图形显示四_第4页
第4页 / 共10页
Java实现最优三角剖分算法并图形显示四_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《Java实现最优三角剖分算法并图形显示四》由会员分享,可在线阅读,更多相关《Java实现最优三角剖分算法并图形显示四(10页珍藏版)》请在金锄头文库上搜索。

1、Java实现最优三角剖分算法并图形显示四/ 使用 Java语言实现最优三角剖分,并图形显示,本程序三个类,一个主测试类,一个算法类,一个图形类import javax.swing.*; import java.awt.*; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.util.LinkedList; import java.util.Scanner; class Mypanel extends JPanel private int length; private int arr;

2、 private LinkedListint list; private static int N=60; private static int move = 100; public Mypanel(int length,int arr,LinkedListint list) this.length=length; this.arr=arr; this.list=list; protected void paintComponent(Graphics g) super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; BasicStroke s

3、troke =new BasicStroke(5); g2.setStroke(stroke); g.setColor(Color.red); for(int j=0;jlist.size();j+) int first =(list.get(j)0; int second = (list.get(j)1; / System.out.println(first); /System.out.println(second); g.drawLine(arrfirst0*N+move,arrfirst1*N+move,arrsecond0*N+move,arrsecond1*N+move); for(

4、int i=0;ilength;i+) String vf; g.setColor(Color.blue); g.drawLine(arri0*N+move,arri1*N+move,arr(i+1)%length0*N+move,arr(i+1)%length1*N+move); g.setColor(Color.black); vf = V+String.valueOf(i); g2.setFont(new Font(, Font.BOLD, 20); g.drawString(vf, arri0*N+move, arri1*N+move); /g.drawString( 贺小涵 , 20

5、0, 50); /* 此是算法类 */ class PolygonTrigangle private int arr; public PolygonTrigangle(int a) arr=a; public float weight(int first, int second, int third) double sum1= Math.sqrt(arrfirst0-arrsecond0)*(arrfirst0-arrsecond0)+(arrfirst1-arrsecond1)*(arrfirst1-arrsecond1); double sum2= Math.sqrt(arrfirst0-

6、arrthird0)*(arrfirst0-arrthird0)+(arrfirst1-arrthird1)*(arrfirst1-arrthird1); double sum3= Math.sqrt(arrthird0-arrsecond0)*(arrthird0-arrsecond0)+(arrthird1-arrsecond1)*(arrthird1-arrsecond1); return (float)(sum1+sum2+sum3); public void MinWeightTri(int n,float t,int s) for(int i=1;i=n;i+) tii=0; fo

7、r(int r=2;r=n;r+) for(int i=1;i=n-r+1;i+) int j=i+r-1; tij=ti+1j+weight(i-1,i,j); sij=i; for(int k=i+1;ki+r-1;k+) float u=tik+tk+1j+weight(i-1,k,j); if(utij) tij=u; sij=k; public class Main public static int arr=new int102; public static float t = new float1010; public static int s = new int1010; pu

8、blic static LinkedListint list =new LinkedListint (); / 这里用来存放弦public static void Trackback(int i,int j, int s) if(i=j) return; if(j-sij=2) int a=sij,j; list.add(a); if(sij-i0) int a=i-1,sij; list.add(a); Trackback(i,sij,s); Trackback(sij+1,j,s); public static void Paint(int length) JFrame f = new J

9、Frame(); f.getContentPane().add(new Mypanel(length,arr,list); f.setTitle(贺小涵 ); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(800,800); f.setVisible(true); public static void main(String args) throws Exception BufferedInputStream in = new BufferedInputStream(new FileInputStream(/input.

10、txt); System.setIn(in); Scanner stdin = new Scanner(System.in); String buf = stdin.nextLine().split( ); int index = Integer.parseInt(buf0); for(int i=0; iindex; i+) arri0=Integer.parseInt(String.valueOf(bufi+1.charAt(1); / 这 里从位置 1 开始,为了统一arri1=Integer.parseInt(String.valueOf(bufi+1.charAt(3); Polyg

11、onTrigangle poly = new PolygonTrigangle(arr); poly.MinWeightTri(index-1, t, s); Trackback(1,index-1,s); Paint(index); / 数据第一个数字代表顶点个数,后面是各个点的坐标/4 (0,0) (1,0) (2,1) (1,1) /9 (0,0) (5,0) (8,1) (9,4) (8,6) (5,7) (3,7) (1,6) (0,2) /10 (0,0) (5,0) (8,1) (9,2) (9,4) (8,6) (5,7) (3,7) (1,6) (0,2) /5 (0,0) (1,0) (2,1) (1,3) (0,2) /6 (0,0) (1,0) (3,2) (3,4) (1,3) (0,2) /7 (0,0) (1,0) (3,1) (4,4) (2,4) (1,3) (0,1) /8 (0,0) (1,0) (4,1) (5,2) (5,4) (3,4) (1,3) (0,2) / 将这些数据让如input.txt 文件中,每次只能读取一行,一行就是一个多边形,然后将三角剖分的图形画出来,要想/ 看其他数据把其他行数据放在第一行就可以了,这个程序是我自己写的,有一点小瑕疵,但不影响最终结果显示/ 读者可以自己尝试了解,三角剖分是剖分的三角形边长和最小

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号