Java是一種非常流行的編程語言,用于開發各種不同類型的應用程序。其中,與點和圓相關的應用程序也廣泛存在。在Java中,點和圓之間的關系可以通過距離公式來計算。
public class Point { private double x; private double y; public Point(double x, double y) { this.x = x; this.y = y; } public double getX() { return x; } public double getY() { return y; } public double distanceTo(Point point) { double dx = point.getX() - x; double dy = point.getY() - y; return Math.sqrt(dx*dx + dy*dy); } } public class Circle { private Point center; private double radius; public Circle(Point center, double radius) { this.center = center; this.radius = radius; } public Point getCenter() { return center; } public double getRadius() { return radius; } public boolean contains(Point point) { double distance = center.distanceTo(point); return (distance<= radius); } } public class Main { public static void main(String[] args) { Point p1 = new Point(0, 0); Point p2 = new Point(3, 4); double distance = p1.distanceTo(p2); System.out.println("Distance between p1 and p2: " + distance); Circle circle = new Circle(new Point(1, 2), 5); System.out.println("Circle contains p1? " + circle.contains(p1)); System.out.println("Circle contains p2? " + circle.contains(p2)); } }
上述代碼定義了一個Point類和Circle類,分別表示點和圓。Point類主要用于計算點與點之間的距離,而Circle類則用于判斷一個點是否在圓內。Main類用于測試上述兩個類的功能,輸出了點之間的距離和點是否在圓內的判斷結果。
上一篇hbiuder php
下一篇css中圓怎么寫