当前页面: 开发资料首页 → J2SE 专题 → 请大家帮帮忙,看看这个程序的允许结果为什么是这个?想了好久都不清楚 
请大家帮帮忙,看看这个程序的允许结果为什么是这个?想了好久都不清楚 
摘要: 请大家帮帮忙,看看这个程序的允许结果为什么是这个?想了好久都不清楚  
import java.io.*;
import static java.lang.Math.sqrt;
public class TryGeometry
{
    public static void main(String[] args)
    {
        Point start = new Point(0.0,1.0);
        Point end = new Point(5.0,6.0);  
        System.out.println("Points created are "+start+" and "+end);
        Line line1 = new Line(start,end);
        Line line2 = new Line(0.0,3.0,3.0,0.0);
        System.out.println("Lines created are "+line1+" and "+line2);
        System.out.println("Intersection is "+line2.intersects(line1));
        end.move(1.0,-5.0);
        System.out.println("Intersection is "+line1.intersects(line2));
    }
}
class Point
{
    double x;
    double y;
    Point(double xVal,double yVal)
    {
        x = xVal;
        y = yVal;
    }
    Point(final Point oldPoint)
    {
        x = oldPoint.x;
        y = oldPoint.y;
    }
    void move(double xDelta,double yDelta)
    {
        x += xDelta;
        y += yDelta;
    }
    double distance(final Point aPoint)
    {
        return sqrt((x - aPoint.x)*(x - aPoint.x)+(y - aPoint.y)*(y - aPoint.y));
    }
    public String toString()
    {
        return Double.toString(x) + ", "+y;
    }
}
class Line
{
    Point start;
    Point end;
    Line(final Point start,final Point end)
    {
        this.start = new Point(start);
        this.end = new Point(end);
    }
    Line(double xStart,double yStart,double xEnd,double yEnd)
    {
        start = new Point(xStart,yStart);
        end = new Point(xEnd,yEnd);
    }
    double length()
    {
        return start.distance(end);
    }
    public String toString()
    {
        return "("+start+"):("+end+")";
    }
    Point intersects(final Line line1)
    {
        Point localPoint = new Point(0,0);
        double num = (this.end.y - this.start.y)*(this.start.x - line1.start.x) -
                     (this.end.x - this.start.x)*(this.start.y - line1.start.y);
        double denom = (this.end.y - this.start.y)*(line1.end.x - line1.start.x) -
                       (this.end.x - this.start.x)*(line1.end.y - line1.start.y);
        localPoint.x = line1.start.x + (line1.end.x - line1.start.x)*num/denom;
        localPoint.y = line1.start.y + (line1.end.y - line1.start.y)*num/denom;
        return localPoint;
    }
}
-------------------------------------------------------------------------
Intersection is:1.0 , 2.0   我计算得到的却是1.0 , 1.2
开始:你line1的两点是(0,1),(5,6),直线是y=x+1;line2的两点是(0,3),(3,0)直线是x+y=3;
交点是(1,2)啊
后来,line1的直线变成乐y=1;line2没有变,所以交点是(2,1)
能不能再说详细一点
改成这样
Line(final Point start,final Point end)
{
this.start = start;
this.end = end;
}
如果照楼主的那样
那么end.move()没有改变直线
怎么楼主的程序我编译时有错误呢?
sqrt 这个好象有问题?难道是我JDK不支持
Points created are 0.0, 1.0 and 5.0, 6.0
Lines created are (0.0, 1.0):(5.0, 6.0) and (0.0, 3.0):(3.0, 0.0)
Intersection is 1.0, 2.0
Intersection is 1.0, 2.0
我这结果