• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            逛奔的蝸牛

            我不聰明,但我會很努力

               ::  :: 新隨筆 ::  ::  :: 管理 ::
            ================================= Triangle.java ==============================


            import java.awt.geom.Point2D;


            /*

             1. 垂心: 三角形三條邊上的高相交于一點.這一點叫做三角形的垂心. 

             2. 重心: 三角形三條邊上的中線交于一點.這一點叫做三角形的重心. 

             3. 外心: 三角形三邊的中垂線交于一點.這一點為三角形外接圓的圓心. 

             4. 內心三角形三內角平分線交于一點.這一點為三角形內切圓的圓心. 


             重心: 三邊上中線的交點 

             垂心: 三條高的交點 

             內心: 內接圓圓心, 三個角角平分線交點 

             外心: 外接圓圓心, 三條邊的垂直平分線交點, 三角形三條邊的垂直平分線的交點


             銳角三角形的外心在三角形內

             直角三角形的外心是斜邊的中點 

             鈍角三角形的外心在三角形外


             a, b, c是三角形的三條邊條

             面積s:   s*s=p(p-a)(p-b)(p-c), p=(a+b+c)/2

             外圓半徑 = abc / (4*面積)

             內圓半徑 = 2 * 面積 / 周長


             內切圓心坐標(x,y): 三角形三個頂點的坐標:A(x1,y1),B(x2,y2),C(x3,y3)

             x=(x1*BC+x2*CA+x3*AB)/(AB+BC+CA);

             y=(y1*BC+y2*CA+y3*AB)/(AB+BC+CA).

             */

            public class Triangle {

                // 三角形的三個頂點

                private Point2D.Double p1;

                private Point2D.Double p2;

                private Point2D.Double p3;


                private double dis12;

                private double dis23;

                private double dis31;


                public Triangle(Point2D.Double p1, Point2D.Double p2, Point2D.Double p3) {

                    this.p1 = p1;

                    this.p2 = p2;

                    this.p3 = p3;


                    dis12 = distenceOfPoints(p1, p2);

                    dis23 = distenceOfPoints(p2, p3);

                    dis31 = distenceOfPoints(p3, p1);

                }


                // 兩點之間的距離

                public static double distenceOfPoints(Point2D.Double p1, Point2D.Double p2) {

                    return Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));

                }


                // 內切圓圓心

                public Point2D.Double innerCenter() {

                    double p = perimeter();


                    double x = (p1.x * dis23 + p2.x * dis31 + p3.x * dis12) / p;

                    double y = (p1.y * dis23 + p2.y * dis31 + p3.y * dis12) / p;


                    return new Point2D.Double(x, y);

                }


                // 外切圓圓心

                public Point2D.Double outerCenter() {

                    double x1 = p1.x;

                    double x2 = p2.x;

                    double x3 = p3.x;

                    double y1 = p1.y;

                    double y2 = p2.y;

                    double y3 = p3.y;

                    double x = ((y2 - y1) * (y3 * y3 - y1 * y1 + x3 * x3 - x1 * x1) - (y3 - y1)

                            * (y2 * y2 - y1 * y1 + x2 * x2 - x1 * x1))

                            / (2 * (x3 - x1) * (y2 - y1) - 2 * ((x2 - x1) * (y3 - y1)));

                    double y = ((x2 - x1) * (x3 * x3 - x1 * x1 + y3 * y3 - y1 * y1) - (x3 - x1)

                            * (x2 * x2 - x1 * x1 + y2 * y2 - y1 * y1))

                            / (2 * (y3 - y1) * (x2 - x1) - 2 * ((y2 - y1) * (x3 - x1)));


                    return new Point2D.Double(x, y);

                }


                // 內切圓半徑

                public double innerRadius() {

                    return (2 * area()) / (dis12 + dis23 + dis31);

                }


                // 外接圓半徑

                public double outerRadius() {

                    return (dis12 * dis23 * dis31) / (4 * area());

                }


                // 三角形的面積

                public double area() {

                    double p = 0.5 * perimeter();

                    return Math.sqrt(p * (p - dis12) * (p - dis23) * (p - dis31));

                }


                // 三角形的周長

                public double perimeter() {

                    return dis12 + dis23 + dis31;

                }


                // 取得三角形的三個頂點

                public Point2D.Double getP1() {

                    return (Point2D.Double) p1.clone();

                }


                public Point2D.Double getP2() {

                    return (Point2D.Double) p2.clone();

                }


                public Point2D.Double getP3() {

                    return (Point2D.Double) p3.clone();

                }

             

            }


            ================================= TriangleDrawer.java ==============================

            import java.awt.Color;

            import java.awt.Dimension;

            import java.awt.Graphics;

            import java.awt.Graphics2D;

            import java.awt.RenderingHints;

            import java.awt.Toolkit;

            import java.awt.geom.Point2D;


            import javax.swing.JFrame;

            import javax.swing.JPanel;

            import javax.swing.SwingUtilities;


            public class TriangleDrawer extends JPanel {

                private static final long serialVersionUID = 1L;

                private Triangle triangle;


                public TriangleDrawer() {

                    triangle = new Triangle(new Point2D.Double(100, 100), new Point2D.Double(207, 130),

                        new Point2D.Double(220, 200));

                }


                @Override

                protected void paintComponent(Graphics g) {

                    super.paintComponent(g);

                    Graphics2D g2d = (Graphics2D) g;

                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);


                    // 繪制三角形

                    drawLine(g, triangle.getP1(), triangle.getP2());

                    drawLine(g, triangle.getP2(), triangle.getP3());

                    drawLine(g, triangle.getP3(), triangle.getP1());


                    // 內接圓

                    double radius = triangle.innerRadius();

                    Point2D.Double center = triangle.innerCenter();

                    g.setColor(Color.BLUE);

                    drawCircle(g, center, radius);


                    // 外接圓

                    radius = triangle.outerRadius();

                    center = triangle.outerCenter();

                    g.setColor(Color.RED);

                    drawCircle(g, center, radius);

                }


                protected void drawLine(Graphics g, Point2D.Double startPoint, Point2D.Double endPoint) {

                    g.drawLine((int) startPoint.x, (int) startPoint.y, (int) endPoint.x, (int) endPoint.y);

                }


                protected void drawCircle(Graphics g, Point2D.Double center, double radius) {

                    g.drawOval((int) (center.x - radius), (int) (center.y - radius), (int) (2 * radius),

                        (int) (2 * radius));

                }


                private static void createGUIAndShow() {

                    JFrame frame = new JFrame("三角形");


                    JPanel contentPane = new TriangleDrawer();

                    frame.setContentPane(contentPane);


                    Dimension ss = Toolkit.getDefaultToolkit().getScreenSize();

                    int w = 500;

                    int h = 500;

                    int x = (ss.width - w) / 2;

                    int y = (ss.height - h) / 2;

                    x = x > 0 ? x : 0;

                    y = y > 0 ? y : 0;

                    frame.setBounds(x, y, w, h);


                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                    frame.setVisible(true);

                }


                public static void main(String[] args) {

                    SwingUtilities.invokeLater(new Runnable() {

                        @Override

                        public void run() {

                            createGUIAndShow();

                        }

                    });

                }

            }

             

            posted on 2010-12-28 22:27 逛奔的蝸牛 閱讀(1777) 評論(0)  編輯 收藏 引用 所屬分類: Java
            亚洲AV日韩精品久久久久| 久久久久国产精品| 久久久久久国产精品无码下载| 久久久久久久免费视频| 综合网日日天干夜夜久久 | 久久精品一本到99热免费| 久久99精品国产麻豆宅宅| 国产精品天天影视久久综合网| 久久久WWW成人| 日韩精品久久无码中文字幕| 免费观看成人久久网免费观看| 中文字幕无码久久人妻| 久久精品国产亚洲AV电影| 理论片午午伦夜理片久久| 国内精品久久久久影院一蜜桃| 亚洲精品综合久久| 伊人久久大香线蕉影院95| 无码国内精品久久人妻| 久久人人爽人人爽AV片| 91精品国产综合久久久久久| 一本色综合久久| 久久99久久无码毛片一区二区| 无码AV中文字幕久久专区| 婷婷国产天堂久久综合五月| 国产精品日韩深夜福利久久| 久久精品www人人爽人人| 伊人久久大香线蕉综合影院首页| 久久无码精品一区二区三区| 四虎国产精品免费久久久| 久久久久人妻一区精品色| 国产A级毛片久久久精品毛片| 亚洲精品乱码久久久久久不卡| 久久国产乱子伦精品免费强| 久久综合噜噜激激的五月天| 囯产极品美女高潮无套久久久| 97久久国产露脸精品国产| 欧美日韩久久中文字幕| 久久久久久精品免费免费自慰| 日本WV一本一道久久香蕉| 国产色综合久久无码有码| 亚洲精品乱码久久久久久久久久久久|