如何找到一个点是在圆圈内部还是外部

时间:2015-11-11 09:36:49

标签: c# oop geometry

我需要弄清楚这个点是在圆圈的内部还是外部。我已经做了一个代码,看看它是否在圆圈上。

但我需要这方面的帮助

  • 我需要创建3个方法来查找是否:

onCircle(Point p),

inCircle(Point p),

outCircle(Point p)

(基本上我需要确定点p是位于圆c,圆内还是圆外。)

要查找它是在圆圈还是在圆圈外,我只是制作一个方法,显示p的距离大于或小于半径。例如:

    public bool onCircle(Point p)
    {
        return dist(p) > radius;
    }

有意义吗?如果我采用x和y的距离并将其与圆的半径进行比较会是正确的吗?

这是我的代码:

public static void Main(string[] args)
{
    Circle c1 = new Circle(0, 0, 1), c2 = new Circle(1, 0, 1);
    Point p1 = new Point();
    p1.setPoint(0, 1);

    p1.print();
    Console.Write(", ");
    c1.print();
    Console.WriteLine();
    if (c1.onCircle(p1))
    {
       // Console.Write(" Point ");
        p1.print();
        Console.Write (" on ");
        c1.print();
        Console.WriteLine();
    }
    else
        Console.WriteLine("Point NOT on circle");

    if (c1.intersect(c2))
    {
       // Console.WriteLine("Two circles intersect");
        c1.print();
        Console.Write(" intersects ");
        c2.print();
        Console.WriteLine();
    }
    else

        Console.WriteLine("Two circles do NOT intersect");
}

我的班级:

class Circle : Point
{
    private double radius;
    public Circle(double xV, double yV, double r)
    {
        this.x = xV;
        this.y = yV;
        this.radius = r;
    }
    public void setRadius(double r)
    {
        radius = r > 0 ? radius : 0;
    }

    public bool onCircle(Point p)
    {
        return dist(p) == radius;
    }

    public bool intersect (Circle c)
    {
        Point p = new Point (), myCenter = new Point();
        p.setPoint (c.x, c.y);
        myCenter.setPoint (x, y);

        return myCenter.dist(p) < radius + c.radius;
    }
    public void print()
    {
        Console.Write("Circle: ({0}, {1}, {2})", x, y, radius);
    }
}

点类:

public class Point
{
    protected double x; // x coordiate of Point
    protected double y; // y coordinate of Point

    public void setPoint (double xCoor, double yCoor) {
        x = xCoor;
        y = yCoor;
    }

    public double dist(Point p)  // compute the distance of point p to the current point
    {
        double distance;
        distance = Math.Sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
        return distance;
    }
    public void print()
    {
        Console.Write("Point: ({0}, {1})", x, y);
    }
}

0 个答案:

没有答案