第1个回答 2011-06-05
我也是新手,写了给你看看:
Point类:
package com.java.tuxing;
public class Point
{
double x;
double y;
public Point(double x,double y)
{
this.x=x;
this.y=y;
}
}
Line类:
package com.java.tuxing;
import java.util.Scanner;
public class Line
{
Point p1,p2;
public Line(Point p1,Point p2){
this.p1=p1;
this.p2=p2;
}
public double LineLength()
{
return Math.sqrt(Math.pow((p2.x-p1.x), 2)+Math.pow((p2.y-p1.y), 2));
}
public String LineName()
{
String str=p1.toString()+p2.toString();
return str.toString();
}
public Point ChangeXY(Point p)
{
System.out.println("输入坐标值 x,y:");
Scanner sc=new Scanner(System.in);
p.x=sc.nextDouble();
p.y=sc.nextDouble();
return p;
}
public double ZhongdianLength()
{
double m=(p1.x+p2.x)/2;
double n=(p1.y+p2.y)/2;
return Math.sqrt(Math.pow(m, 2)+Math.pow(n, 2));
}
}
Triangle类:
package com.java.tuxing;
public class Triangle {
Point p1,p2,p3;
Line l1=new Line(p1,p2);
Line l2=new Line(p1,p3);
Line l3=new Line(p2,p3);
public Triangle(Point p1,Point p2,Point p3)
{
this.p1=p1;
this.p2=p2;
this.p3=p3;
}
public double bianchang(Point p1,Point p2)
{
return Math.sqrt(Math.pow((p2.x-p1.x), 2)+Math.pow((p2.y-p1.y), 2));
}
public double mianji()
{
double p12=Math.sqrt(Math.pow((p2.x-p1.x), 2)+Math.pow((p2.y-p1.y), 2));
double p13=Math.sqrt(Math.pow((p3.x-p1.x), 2)+Math.pow((p3.y-p1.y), 2));
double p23=Math.sqrt(Math.pow((p3.x-p2.x), 2)+Math.pow((p3.y-p2.y), 2));
double pp=(p12+p13+p23)/2;
return Math.sqrt(pp*(pp-p12)*(pp-p13)*(pp-p23));
}
public boolean isTriangle()
{
if((p2.y-p1.y)/(p2.x-p1.x)==(p3.y-p1.y)/(p3.x-p1.x)) //判断两直线斜率,不要弄特殊点以免斜率不存在
{
return false;
}
else
return true;
}
}
测试类:
package com.java.tuxing;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Point X=new Point(1,1);
Point Y=new Point(4,4);
Point A=new Point(1,1);
Point B=new Point(2,3);
Point C=new Point(3,4);
Line L1=new Line(X,Y);
Triangle T=new Triangle(A,B,C);
double xylength=L1.LineLength();
String xyname=L1.LineName();
double zdlength=L1.ZhongdianLength();
double ablength=T.bianchang(A, B);
double aclength=T.bianchang(A, C);
double bclength=T.bianchang(B, C);
double abcarea=T.mianji();
boolean bl=T.isTriangle();
System.out.println("点:------------------------------------------");
System.out.println("点X的坐标是: ("+X.x+","+X.y+")");
System.out.println("点Y的坐标是: ("+Y.x+","+Y.y+")");
System.out.println("直线:-----------------------------------------");
System.out.println("点X的坐标是: ("+X.x+","+X.y+")");
System.out.println("点Y的坐标是: ("+Y.x+","+Y.y+")");
System.out.println("XY的边长是:"+xylength);
System.out.println("XY的边长名称是:"+xyname);
System.out.println("XY的边中点距离原点:"+zdlength);
System.out.println("三角形:---------------------------------------");
System.out.println("点A的坐标是: ("+A.x+","+A.y+")");
System.out.println("点B的坐标是: ("+B.x+","+B.y+")");
System.out.println("点C的坐标是: ("+C.x+","+C.y+")");
System.out.println("AB的边长是:"+ablength);
System.out.println("AC的边长是:"+aclength);
System.out.println("BC的边长是:"+bclength);
System.out.println("三角形ABC的面积是:"+abcarea);
System.out.println("T是三角形吗?:"+bl);
}
}
输出结果:
点:------------------------------------------
点X的坐标是: (1.0,1.0)
点Y的坐标是: (4.0,4.0)
直线:-----------------------------------------
点X的坐标是: (1.0,1.0)
点Y的坐标是: (4.0,4.0)
XY的边长是:4.242640687119285
XY的边长名称是:com.java.tuxing.Point@61de33com.java.tuxing.Point@14318bb
XY的边中点距离原点:3.5355339059327378
三角形:---------------------------------------
点A的坐标是: (1.0,1.0)
点B的坐标是: (2.0,3.0)
点C的坐标是: (3.0,4.0)
AB的边长是:2.23606797749979
AC的边长是:3.605551275463989
BC的边长是:1.4142135623730951
三角形ABC的面积是:0.5000000000000032
T是三角形吗?:true
直线名称不知道怎么弄...
第2个回答 2011-06-05
/**
* @author shy2850
*/
public class Point {
/**点的名称*/
private Character name;
public float x;
public float y;
public Point(float x, float y) {
this('P',x,y);
}
public Point(Character name, float x, float y) {
this.name = name;
this.x = x;
this.y = y;
}
public Point() {
}
public void setName(Character name) {
this.name = name;
}
public Character getName() {
return name;
}
public String toString() {
return name+"(" + x + "," + y + ")";
}
}
public class Line {
private Point beginPoint;
private Point endPoint;
public Line(Point beginPoint, Point endPoint) {
this.beginPoint = beginPoint;
this.endPoint = endPoint;
}
public String getName(){
if(null == beginPoint.getName() || null == endPoint.getName())throw new RuntimeException("线段中的点缺少名称!");
return beginPoint.getName()+""+endPoint.getName();
}
public Point getBeginPoint() {
return beginPoint;
}
public void setBeginPoint(Point beginPoint) {
this.beginPoint = beginPoint;
}
public Point getEndPoint() {
return endPoint;
}
public void setEndPoint(Point endPoint) {
this.endPoint = endPoint;
}
public double getLength(){
return Math.sqrt((beginPoint.x-endPoint.x)*(beginPoint.x-endPoint.x)+(beginPoint.y-endPoint.y)*(beginPoint.y-endPoint.y));
}
public double getDistenceOfCenter(){
return Math.sqrt((beginPoint.x+endPoint.x)*(beginPoint.x+endPoint.x)+(beginPoint.y+endPoint.y)*(beginPoint.y+endPoint.y))/4;
}
public String toString() {
return "Line:"+getName();
}
}
public class Trangle {
private Point p1;
private Point p2;
private Point p3;
private Line[] lines = new Line[3];
public Point getP1() {
return p1;
}
public void setP1(Point p1) {
this.p1 = p1;
}
public Point getP2() {
return p2;
}
public void setP2(Point p2) {
this.p2 = p2;
}
public Point getP3() {
return p3;
}
public void setP3(Point p3) {
this.p3 = p3;
}
public Trangle(Point p1, Point p2, Point p3) {
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}
public Line[] getLines(){
if(null == lines){
lines[0] = new Line(p1, p2);
lines[1] = new Line(p2, p3);
lines[2] = new Line(p3, p1);
}
return lines;
}
public boolean isTrangle(){
Line[] lines = getLines();
double l0 = lines[0].getLength();
double l1 = lines[1].getLength();
double l2 = lines[2].getLength();
return (l0+l1>l2)&&(l0+l2>l1)&&(l1+l2>l0);
}
public double getArea(){
Line[] lines = getLines();
if(!isTrangle())throw new RuntimeException("不是三角形!");
double l0 = lines[0].getLength();
double l1 = lines[1].getLength();
double l2 = lines[2].getLength();
double s = (l0+l1+l2)/2;
return Math.sqrt(s*(s-l0)*(s-l1)*(s-l2));
}
public String toString() {
return "Trangle:△"+p1.getName()+p2.getName()+p3.getName();
}
}