import java.awt.geom.Line2D;
public class Triangle2D {
public MyPoint p1 ;
public MyPoint p2 ;
public MyPoint p3 ;
public MyPoint getP1() {
return p1;
}
public void setP1(MyPoint p1) {
this.p1 = p1;
}
public MyPoint getP2() {
return p2;
}
public void setP2(MyPoint p2) {
this.p2 = p2;
}
public MyPoint getP3() {
return p3;
}
public void setP3(MyPoint p3) {
this.p3 = p3;
}
public Triangle2D() {
this(0.0,0.0,1.0,1.0,2.0,5.0);
}
public Triangle2D(MyPoint p1, MyPoint p2, MyPoint p3) {
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}
public Triangle2D(double x, double y, double x1, double y1, double x2, double y2) {
p1=new MyPoint(x,y);
p2=new MyPoint(x1,y1);
p3=new MyPoint(x2,y2);
}
public double getArea(MyPoint p1,MyPoint p2,MyPoint p3){
double w,e,r,t;
w = p1.distance(p1,p2);
e = p2.distance(p1,p3);
r = p3.distance(p2,p3);
t = (w + e + r) / 2.0;
return (t * (t -w) * (t - e) * (t - r));
}
public double getPerimeter(MyPoint p1,MyPoint p2,MyPoint p3){
double w,e,r;
w = p1.distance(p1,p2);
e = p2.distance(p1,p3);
r = p3.distance(p2,p3);
return ( w + e + r);
}
public int contains(MyPoint p){
Line2D.Double c = new Line2D.Double(p1.getX(), p1.getY(), p2.getX(), p2.getY());
Line2D.Double c1 = new Line2D.Double(p1.getX(), p1.getY(), p3.getX(), p3.getY());
Line2D.Double c2 = new Line2D.Double(p2.getX(), p2.getY(), p3.getX(), p3.getY());
Line2D.Double c3 = new Line2D.Double(p1.getX(), p1.getY(), p.getX(), p.getY());
Line2D.Double c4 = new Line2D.Double(p2.getX(), p2.getY(), p.getX(), p.getY());
Line2D.Double c5 = new Line2D.Double(p3.getX(), p3.getY(), p.getX(), p.getY());
if(c.contains(p.getX(), p.getY())||c1.contains(p.getX(), p.getY())||c2.contains(p.getX(), p.getY()))
return 0;
if(LineUtil.isAPoint(p1, p2, p3, p))
return 1;
if(c.intersectsLine(c5))
return 3;
else if(c1.intersectsLine(c3))
return 3;
else if(c2.intersectsLine(c4))
return 3;
else if(c.ptLineDist(p.getX(), p.getY())<c.ptLineDist(p3.getX(),p3.getY())
&&c1.ptLineDist(p.getX(), p.getY())<c1.ptLineDist(p1.getX(),p1.getY())
&&c2.ptLineDist(p.getX(), p.getY())<c2.ptLineDist(p2.getX(),p2.getY()))
return 2;
else
return 3;
}
public void overlaps(Triangle2D t){
int i=contains(t.getP1());
int j=contains(t.getP2());
int k=contains(t.getP3());
if(i==3&&j==3&&k==3){
System.out.println("out of the triangle");
}
else
System.out.println("in the triangle");
//return false;
}
}
//
class LineUtil{
public double getK(MyPoint p0,MyPoint p1){
return (p0.getY()-p1.getY())/(p0.getX()-p1.getX());
}
public double getK(MyPoint p0,double x,double y){
return (p0.getY()-y)/(p0.getX()-x);
}
public static boolean isAPoint(MyPoint p1,MyPoint p2,MyPoint p3,MyPoint thePointToTest){
if((p1.getX()==thePointToTest.getX()&&p1.getY()==thePointToTest.getY())
||(p2.getX()==thePointToTest.getX()&&p2.getY()==thePointToTest.getY())
||(p3.getX()==thePointToTest.getX()&&p3.getY()==thePointToTest.getY())){
return true;
}
else
return false;
}
}
//建立点集,具有横纵坐标的点。
class MyPoint{
private double x,y;
public double getX() {
return x;
}
public double getY() {
return y;
}
public MyPoint() {
this.x = 0;
this.y = 0;
}
public MyPoint(double x, double y) {
this.x = x;
this.y = y;
}
public double distance(MyPoint a,MyPoint b){
return Math.sqrt((a.getX()-b.getX())*(a.getX()-b.getX())+(a.getY()-b.getY())*(a.getY() -b.getY()));
}
public double distance(double x1,double x2,double y1, double y2){
return Math.sqrt((y1 -y2) *(y1 -y2)+(x1 -x2)*(x1 -x2));
}
public class Triangle2D {
public MyPoint p1 ;
public MyPoint p2 ;
public MyPoint p3 ;
public MyPoint getP1() {
return p1;
}
public void setP1(MyPoint p1) {
this.p1 = p1;
}
public MyPoint getP2() {
return p2;
}
public void setP2(MyPoint p2) {
this.p2 = p2;
}
public MyPoint getP3() {
return p3;
}
public void setP3(MyPoint p3) {
this.p3 = p3;
}
public Triangle2D() {
this(0.0,0.0,1.0,1.0,2.0,5.0);
}
public Triangle2D(MyPoint p1, MyPoint p2, MyPoint p3) {
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}
public Triangle2D(double x, double y, double x1, double y1, double x2, double y2) {
p1=new MyPoint(x,y);
p2=new MyPoint(x1,y1);
p3=new MyPoint(x2,y2);
}
public double getArea(MyPoint p1,MyPoint p2,MyPoint p3){
double w,e,r,t;
w = p1.distance(p1,p2);
e = p2.distance(p1,p3);
r = p3.distance(p2,p3);
t = (w + e + r) / 2.0;
return (t * (t -w) * (t - e) * (t - r));
}
public double getPerimeter(MyPoint p1,MyPoint p2,MyPoint p3){
double w,e,r;
w = p1.distance(p1,p2);
e = p2.distance(p1,p3);
r = p3.distance(p2,p3);
return ( w + e + r);
}
public int contains(MyPoint p){
Line2D.Double c = new Line2D.Double(p1.getX(), p1.getY(), p2.getX(), p2.getY());
Line2D.Double c1 = new Line2D.Double(p1.getX(), p1.getY(), p3.getX(), p3.getY());
Line2D.Double c2 = new Line2D.Double(p2.getX(), p2.getY(), p3.getX(), p3.getY());
Line2D.Double c3 = new Line2D.Double(p1.getX(), p1.getY(), p.getX(), p.getY());
Line2D.Double c4 = new Line2D.Double(p2.getX(), p2.getY(), p.getX(), p.getY());
Line2D.Double c5 = new Line2D.Double(p3.getX(), p3.getY(), p.getX(), p.getY());
if(c.contains(p.getX(), p.getY())||c1.contains(p.getX(), p.getY())||c2.contains(p.getX(), p.getY()))
return 0;
if(LineUtil.isAPoint(p1, p2, p3, p))
return 1;
if(c.intersectsLine(c5))
return 3;
else if(c1.intersectsLine(c3))
return 3;
else if(c2.intersectsLine(c4))
return 3;
else if(c.ptLineDist(p.getX(), p.getY())<c.ptLineDist(p3.getX(),p3.getY())
&&c1.ptLineDist(p.getX(), p.getY())<c1.ptLineDist(p1.getX(),p1.getY())
&&c2.ptLineDist(p.getX(), p.getY())<c2.ptLineDist(p2.getX(),p2.getY()))
return 2;
else
return 3;
}
public void overlaps(Triangle2D t){
int i=contains(t.getP1());
int j=contains(t.getP2());
int k=contains(t.getP3());
if(i==3&&j==3&&k==3){
System.out.println("out of the triangle");
}
else
System.out.println("in the triangle");
//return false;
}
}
//
class LineUtil{
public double getK(MyPoint p0,MyPoint p1){
return (p0.getY()-p1.getY())/(p0.getX()-p1.getX());
}
public double getK(MyPoint p0,double x,double y){
return (p0.getY()-y)/(p0.getX()-x);
}
public static boolean isAPoint(MyPoint p1,MyPoint p2,MyPoint p3,MyPoint thePointToTest){
if((p1.getX()==thePointToTest.getX()&&p1.getY()==thePointToTest.getY())
||(p2.getX()==thePointToTest.getX()&&p2.getY()==thePointToTest.getY())
||(p3.getX()==thePointToTest.getX()&&p3.getY()==thePointToTest.getY())){
return true;
}
else
return false;
}
}
//建立点集,具有横纵坐标的点。
class MyPoint{
private double x,y;
public double getX() {
return x;
}
public double getY() {
return y;
}
public MyPoint() {
this.x = 0;
this.y = 0;
}
public MyPoint(double x, double y) {
this.x = x;
this.y = y;
}
public double distance(MyPoint a,MyPoint b){
return Math.sqrt((a.getX()-b.getX())*(a.getX()-b.getX())+(a.getY()-b.getY())*(a.getY() -b.getY()));
}
public double distance(double x1,double x2,double y1, double y2){
return Math.sqrt((y1 -y2) *(y1 -y2)+(x1 -x2)*(x1 -x2));
}
}
//测试程序
public class TextTriangle2D {
public static void main(String[] args) {
MyPoint p=new MyPoint(4,5);
MyPoint p1=new MyPoint(2,6);
MyPoint p2=new MyPoint(4,7);
MyPoint p3=new MyPoint(5,3);
Triangle2D t1 = new Triangle2D(p1,p2,p3);
Triangle2D t2 = new Triangle2D();
System.out.println(t1.contains(p));
t1.overlaps(t2);
System.out.println(t1.getPerimeter(p1,p2,p3));
System.out.println(t1.getArea(p1, p2, p3));
}
}