package test;
import java.awt.geom.Line2D;
import javax.swing.JOptionPane;
public class MyLine {
private Point e1,e2;
public MyLine() {
e1 = new Point(0.0 , 0.0);
e2 = new Point(0.0 , 0.0);
}
public MyLine(Point p1 , Point p2) {
e1 = new Point(p1);
e2 = new Point(p2);
}
public MyLine(Point p1) {
e1 = new Point(0 , 0);
e2 = new Point(p1);
}
public Point getE1() {
return e1;
}
public void setE1(Point e1) {
this.e1 = e1;
}
public Point getE2() {
return e2;
}
public void setE2(Point e2) {
this.e2 = e2;
}
//线段是否在第一象限
public boolean check(){
if(e1.getX() > 0 && e1.getY() > 0 && e2.getX() > 0 && e2.getY() > 0){
return true;
}
else
return false;
}
//线段长度
double length(){
double leng;
leng = Math.sqrt( (e1.getX()-e2.getX())*(e1.getX()-e2.getX()) +
(e1.getY()-e2.getY())*(e1.getY()-e2.getY())
);
return leng;
}
//判断两条线是否交叉
boolean intersect(MyLine line2){
boolean result;
//Line2D一个java自带类库
//linesIntersect(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
//测试从 (x1,y1) 到 (x2,y2) 的线段是否与从 (x3,y3) 到 (x4,y4) 的线段相交。
//只需要传入两个线段的两个【端点(两个坐标,4个数)】就知道2线段交叉收否
result = Line2D.linesIntersect(
getE1().getX() , getE1().getY() ,
getE2().getX() , getE2().getY() ,
line2.getE1().getX() , line2.getE1().getY() ,
line2.getE2().getX() , line2.getE2().getY()
);
return result;
}
//点到线的距离
double distance(Point p){
double result;
result = Line2D.ptLineDist(
getE1().getX(), getE1().getY(), getE2().getX(), getE2().getY(), //线
p.getX(), p.getY() //点
);
return result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean result;
MyLine line1 , line2;
String s = "请在下面输入第一条直线的两点的坐标。形式(x1,y1,x2,y2)";
//s1,s2,s3,s4是输入的值
String s1 = JOptionPane.showInputDialog(s + "\n第一个点的横坐标x1");
String s2 = JOptionPane.showInputDialog(s + "\n第一个点的纵坐标y1");
String s3 = JOptionPane.showInputDialog(s + "\n第二个个点的横坐标x2");
String s4 = JOptionPane.showInputDialog(s + "\n第二个点的横坐标y2");
Point pone = new Point(Double.parseDouble(s1) , Double.parseDouble(s2));
Point ptwo = new Point(Double.parseDouble(s3) , Double.parseDouble(s4));
s = "请在下面对话框输入第二条直线的两点坐标";
s1 = JOptionPane.showInputDialog(s + "\n第一个点的横坐标x1");
s2 = JOptionPane.showInputDialog(s + "\n第一个点的纵坐标y1");
s3 = JOptionPane.showInputDialog(s + "\n第二个个点的横坐标x2");
s4 = JOptionPane.showInputDialog(s + "\n第二个点的横坐标y2");
Point pthree = new Point(Double.parseDouble(s1) , Double.parseDouble(s2));
Point pfour = new Point(Double.parseDouble(s3) , Double.parseDouble(s4));
s1 = JOptionPane.showInputDialog("请在下面对话框输入一个点的横坐标");
s2 = JOptionPane.showInputDialog("请在下面对话框输入一个点的纵坐标");
Point pfive = new Point(Double.parseDouble(s1) , Double.parseDouble(s2));
line1 = new MyLine(pone , ptwo);
line2 = new MyLine(pthree , pfour);
//***象限
result = line1.check();//是否在第一象限
if(result){
System.out.println("line1 在第一象限");
} else{
System.out.println("line1不在第一象限");
}
//***长度
double length = line1.length();
System.out.println("line1的长度是:"+length);
//***相交
result = line1.intersect(line2);
if(result){
System.out.println("line1和line2相交");
} else{
System.out.println("line1和line2不相交");
}
//***点到线的距离
double distan = line1.distance(pfive);
System.out.println("点到line1的距离是:" + distan);
}
}
class Point {
private double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public Point() {
this(0, 0);
}
public Point(Point p) {
this(p.getX(), p.getY());
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public void moveTo(double x1, int y1) {
x = x1;
y = y1;
}
}
线段与点关系的代码
猜你喜欢
转载自blog.csdn.net/qq_38261174/article/details/80631723
今日推荐
周排行