- package com.mingrui.soft_struct;
- import java.awt.geom.Line2D;
- public class Triangle2D{
- private MyPoint p0;
- private MyPoint p1;
- private MyPoint p2;
- Triangle2D(){
- this(0.0,0.0,1.0,1.0,2.0,5.0);
- }
- Triangle2D(double x,double y,double x1,double y1,double x2,double y2){
- p0=new MyPoint(x,y);
- p1=new MyPoint(x1,y1);
- p2=new MyPoint(x2,y2);
- }
- public MyPoint getP0(){
- return p0;
- }
- public MyPoint getP1(){
- return p1;
- }
- public MyPoint getP2(){
- return p2;
- }
- public double getArea(){
- double p=getPerimeter()/2.0;
- return Math.sqrt(p*
- (p-MyPoint.distance(p0,p1))*
- (p-MyPoint.distance(p1,p2))*
- (p-MyPoint.distance(p0,p2)));
- }
- public double getPerimeter(){
- return MyPoint.distance(p0,p1)+MyPoint.distance(p1,p2)+MyPoint.distance(p2,p0);
- }
- public int contains(MyPoint p){
- /*
- 0. in the line
- 1.is a point
- 2.in the triangle
- 3.out of the triangle
- */
- Line2D.Double ld = new Line2D.Double(p0.getX(), p0.getY(), p1.getX(), p1.getY());
- Line2D.Double ld1 = new Line2D.Double(p1.getX(), p1.getY(), p2.getX(), p2.getY());
- Line2D.Double ld2 = new Line2D.Double(p0.getX(), p0.getY(), p2.getX(), p2.getY());
- Line2D.Double ld3 = new Line2D.Double(p0.getX(), p0.getY(), p.getX(), p.getY());
- Line2D.Double ld4 = new Line2D.Double(p1.getX(), p1.getY(), p.getX(), p.getY());
- Line2D.Double ld5 = new Line2D.Double(p2.getX(), p2.getY(), p.getX(), p.getY());
- if(ld.contains(p.getX(), p.getY())||ld1.contains(p.getX(), p.getY())||ld2.contains(p.getX(), p.getY()))
- return 0;
- if(LineUtil.isAPoint(p0, p1, p2, p))
- return 1;
- if(ld.intersectsLine(ld5))
- return 3;
- else if(ld1.intersectsLine(ld3))
- return 3;
- else if(ld2.intersectsLine(ld4))
- return 3;
- else if(ld.ptLineDist(p.getX(), p.getY())<ld.ptLineDist(p2.getX(),p2.getY())
- &&ld1.ptLineDist(p.getX(), p.getY())<ld1.ptLineDist(p0.getX(),p0.getY())
- &&ld2.ptLineDist(p.getX(), p.getY())<ld2.ptLineDist(p1.getX(),p1.getY()))
- return 2;
- else
- return 3;
- }
- public void overlaps(Triangle2D t){
- int i=contains(t.getP0());
- int j=contains(t.getP1());
- int k=contains(t.getP2());
- /*
- * 0. in the line
- * 1.is a point
- * 2.in the triangle
- * 3.out of the triangle
- */
- if(i==3&&j==3&&k==3){
- System.out.println("out of the triangle");
- }
- else
- System.out.println("in the triangle");
- //return false;
- }
- public static void main(String args[]){
- Triangle2D t1=new Triangle2D();
- Triangle2D t2=new Triangle2D();
- MyPoint p=new MyPoint(1,1.3);
- System.out.println(t1.contains(p));
- t1.overlaps(t2);
- }
- }
- class MyPoint{
- private double x;
- private double y;
- MyPoint(){
- this.x=0;
- this.y=0;
- }
- MyPoint(double x,double y){
- this.x=x;
- this.y=y;
- }
- public double getX(){
- return x;
- }
- public double getY(){
- return y;
- }
- public static double distance(MyPoint p,MyPoint p1){
- return Math.sqrt((p.getX()-p1.getX())*(p.getX()-p1.getX())+(p.getY()-p1.getY())*(p.getY()-p1.getY()));
- }
- public static double distance(int x,int y,int x1,int y1){
- return Math.sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1));
- }
- }
- class LineUtil{
- public static double getK(MyPoint p0,MyPoint p1){
- return (p0.getY()-p1.getY())/(p0.getX()-p1.getX());
- }
- public static double getK(MyPoint p0,double x,double y){
- return (p0.getY()-y)/(p0.getX()-x);
- }
- public static boolean isAPoint(MyPoint p0,MyPoint p1,MyPoint p2,MyPoint thePointToTest){
- if((p0.getX()==thePointToTest.getX()&&p0.getY()==thePointToTest.getY())
- ||(p1.getX()==thePointToTest.getX()&&p1.getY()==thePointToTest.getY())
- ||(p2.getX()==thePointToTest.getX()&&p2.getY()==thePointToTest.getY())){
- return true;
- }
- else
- return false;
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/090520149492.html
来源: http://www.codesnippet.cn/detail/090520149492.html