Класс Point:
public class Point {
private double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return String.format("(x,y) is (%.1f,%.1f)", x, y);
public void addX(double x) {
this.x += x;
public void addY(double y) {
this.y += y;
public void add(double x, double y) {
addX(x);
addY(y);
public double dstToCenter() {
return Math.sqrt(x*x + y*y);
============================================
Класс Segment
public class Segment {
private final Point p1, p2;
public Segment(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
public Segment(double x1, double y1, double x2, double y2) {
this.p1 = new Point(x1, y1);
this.p2 = new Point(x2, y2);
return String.format("1st %s and 2nd %s", p1.toString(), p2.toString());
public boolean isOnSegment(Point p) {
if (p.getX() < Math.min(p1.getX(), p2.getX()) || p.getX() > Math.max(p1.getX(), p2.getX())
|| p.getY() < Math.min(p1.getY(), p2.getY()) || p.getY() > Math.max(p1.getY(), p2.getY())) return false;
double A = p2.getY() - p1.getY();
double B = p1.getX() - p2.getX();
double C = p1.getX() * p2.getY() - p2.getX() * p1.getY();
return A * p.getX() + B * p.getY() + C == 0;
public double getLength() {
return Math.sqrt((p2.getX()-p1.getX())*(p2.getX()-p1.getX()) + (p2.getY()-p1.getY())*(p2.getY()-p1.getY()));
public double getAngleToX() {
if (p1.getX() == p2.getX()) return 90;
double A = Math.atan2(p1.getY() - p2.getY(), p1.getX() - p2.getX()) / Math.PI * 180;
return A < 0 ? A + 360 : A;
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
Класс Point:
public class Point {
private double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return String.format("(x,y) is (%.1f,%.1f)", x, y);
}
public void addX(double x) {
this.x += x;
}
public void addY(double y) {
this.y += y;
}
public void add(double x, double y) {
addX(x);
addY(y);
}
public double dstToCenter() {
return Math.sqrt(x*x + y*y);
}
}
============================================
Класс Segment
public class Segment {
private final Point p1, p2;
public Segment(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
}
public Segment(double x1, double y1, double x2, double y2) {
this.p1 = new Point(x1, y1);
this.p2 = new Point(x2, y2);
}
@Override
public String toString() {
return String.format("1st %s and 2nd %s", p1.toString(), p2.toString());
}
public boolean isOnSegment(Point p) {
if (p.getX() < Math.min(p1.getX(), p2.getX()) || p.getX() > Math.max(p1.getX(), p2.getX())
|| p.getY() < Math.min(p1.getY(), p2.getY()) || p.getY() > Math.max(p1.getY(), p2.getY())) return false;
double A = p2.getY() - p1.getY();
double B = p1.getX() - p2.getX();
double C = p1.getX() * p2.getY() - p2.getX() * p1.getY();
return A * p.getX() + B * p.getY() + C == 0;
}
public double getLength() {
return Math.sqrt((p2.getX()-p1.getX())*(p2.getX()-p1.getX()) + (p2.getY()-p1.getY())*(p2.getY()-p1.getY()));
}
public double getAngleToX() {
if (p1.getX() == p2.getX()) return 90;
double A = Math.atan2(p1.getY() - p2.getY(), p1.getX() - p2.getX()) / Math.PI * 180;
return A < 0 ? A + 360 : A;
}
}