java 判断一个平面两条直线是否平行
两点成一线,两线成一面
站在用户的角度思考问题,与客户深入沟通,找到平泉网站设计与平泉网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站设计、成都做网站、企业官网、英文网站、手机端网站、网站推广、域名与空间、网站空间、企业邮箱。业务覆盖平泉地区。
如果在坐标系里面,可以根据坐标判断啊
java编程判断这两条直线是否相交?,若相交,求出交点。
您好,下面是我写的代码,麻烦看一下是否符合要求。
Point类:
public class Point {
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
Line类:
public class Line {
private Point a;
private Point b;
public Point getA() {
return a;
}
public Point getB() {
return b;
}
public Line(Point a, Point b) {
this.a = a;
this.b = b;
}
}
LineIntersect类:
public class LineIntersect {
private Line ab;
private Line cd;
public LineIntersect(Line ab, Line cd) {
this.ab = ab;
this.cd = cd;
}
public void JudgeLineIntersect() {
Point A = this.ab.getA();
Point B = this.ab.getB();
Point C = this.cd.getA();
Point D = this.cd.getB();
int d1 = (B.getY() - A.getY()) * (D.getX() - C.getX());
int d2 = (B.getX() - A.getX()) * (D.getY() - C.getY());
int d3 = (C.getY() - A.getY()) * (B.getX() - A.getX());
int d4 = (C.getX() - A.getX()) * (B.getY() - A.getY());
if (d1 == d2) {
System.out.println("两条直线平行。");
} else if (d3 == d4) {
System.out.println("两条直线重合。");
} else {
System.out.println("两条直线相交。");
int d5 = C.getY() * D.getX() - C.getX() * D.getY();
int d6 = A.getY() * B.getX() - A.getX() * B.getY();
float intersect_x = ((C.getX() - D.getX()) * d6 - (A.getX() - B.getX()) * d5)
/ ((B.getY() - A.getY()) * (D.getX() - C.getX()) - (B.getX() - A.getX()) * (D.getY() - C.getY()));
float intersect_y = ((C.getY() - D.getY()) * d6 - (A.getY() - B.getY()) * d5)
/ ((B.getY() - A.getY()) * (D.getX() - C.getX()) - (B.getX() - A.getX()) * (D.getY() - C.getY()));
System.out.println("交点的X坐标为:" + intersect_x);
System.out.println("交点的Y坐标为:" + intersect_y);
}
}
}
下面是主类(测试类):
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.print("请输入A点的X坐标:");
Scanner inAX = new Scanner(System.in);
int ax = inAX.nextInt();
System.out.print("请输入A点的Y坐标:");
Scanner inAY = new Scanner(System.in);
int ay = inAY.nextInt();
System.out.print("请输入B点的X坐标:");
Scanner inBX = new Scanner(System.in);
int bx = inBX.nextInt();
System.out.print("请输入B点的Y坐标:");
Scanner inBY = new Scanner(System.in);
int by = inBY.nextInt();
System.out.print("请输入C点的X坐标:");
Scanner inCX = new Scanner(System.in);
int cx = inCX.nextInt();
System.out.print("请输入C点的Y坐标:");
Scanner inCY = new Scanner(System.in);
int cy = inCY.nextInt();
System.out.print("请输入D点的X坐标:");
Scanner inDX = new Scanner(System.in);
int dx = inDX.nextInt();
System.out.print("请输入D点的Y坐标:");
Scanner inDY = new Scanner(System.in);
int dy = inDY.nextInt();
Point A = new Point();
A.setX(ax);
A.setY(ay);
Point B = new Point();
B.setX(bx);
B.setY(by);
Point C = new Point();
C.setX(cx);
C.setY(cy);
Point D = new Point();
D.setX(dx);
D.setY(dy);
Line AB = new Line(A, B);
Line CD = new Line(C, D);
LineIntersect lineIntersect = new LineIntersect(AB, CD);
lineIntersect.JudgeLineIntersect();
inAX.close();
inAY.close();
inBX.close();
inBY.close();
inCX.close();
inCY.close();
inDX.close();
inDY.close();
}
}
下面是我刚才做的测试结果:
java输入四个坐标判断是否为平行四边形并显示该图形.QQ847507674会的大侠教下急用
找下平行四边形判定条件,根据条件判定就行了啊!至于作图,你在判断的过程中就能知道哪些顶点应该相连,在该相连的顶点间作直线就可以了。
用JAVA语言证明两条直线平行?
两直线平行的条件:
如果两条直线的斜率都存在, 并且相等, 那么这两条直线平行. (y=2x + 1 and y=2x - 1)
如果两条直线的斜率都不存在, 那么这两条直线平行. (x = 1 and x= -1)
以上有2种特殊情况需要注意:
a. 斜率都为0, 例如 y= 1 and y= -1.
b. 斜率不存在, 例如 x=1 and x= -1.
这时, 两直线也都平行.
那么用java 实现, 我们就需要用户以y=kx + b, 中的k值, 即斜率.
import java.util.Scanner;
public class ParallelismDemo {
public static void main(String args[])
{
new ParallelismDemo();
}
//Constructor method, will run during newing instance of this class.
public ParallelismDemo()
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter first line's value 'k' which format is y = kx + b (If not exist, enter n)");
String firstValueK = scan.nextLine();
System.out.println("Please enter second line's value 'k' which format is y = kx + b (If not exist, enter n)");
String secValueK = scan.nextLine();
scan.close();
//Check both k are not exist
if(firstValueK.equals("n") secValueK.equals("n"))
{
System.out.println("Your lines are parallel");
return;
}
// If both k exist, then check whether they are equals
else if(firstValueK.equals(secValueK))
{
System.out.println("Your lines are parallel");
return;
}
// For the rest condition, they are not parallel
else
{
System.out.println("Your lines are not parallel");
return;
}
}
}
java求高手改错
其实如一楼所说这种最好还是用2个点定义一条直线必要好点。但是根据题目的要求。我完善和更改了一些漏洞。都写了注释的。数学不是很好,可能还有一些地方没有想到。代码如下:
=====================================================
public class Line {
private Point point;
private double slope;
public Line(Point point, double slope) {
this.setPoint(point);
this.setSlope(slope);
}
public Line(Point p1, Point p2) {
this(p1, 0);
if(p1.getX() == p2.getX()){ // 判断是否斜率存在。不存在斜率设置为无穷大 否则设置为计算所得斜率
this.setSlope(Double.MAX_VALUE);
}
else{
this.setSlope((double)(p2.getY() - p1.getY())/(double)(p2.getX() - p1.getX()));
}
}
public Line(int a, int b) {
this(new Point(a, 0), new Point(0, b));
}
public void setSlope(double slope) {
this.slope = slope;
}
public void setPoint(Point point) {
this.point = point;
}
public double getSlope() {
return slope;
}
public Point getPoint() {
return point;
}
public boolean isParallel(Line line) {
if(this.point.isSamePoint(line.point)){ // 同一条直线重合 或者已经相交 故不平行
return false;
}
else{
if(this.isPointOnLine(line.point)){ // 如果两条直线的定义点都在各自的线上那么说明他们重合不平行
return false;
}
else{
return this.slope == line.slope; //否则才判断斜率是否相等
}
}
}
public boolean isPointOnLine(Point p){
double tmp = (double)(this.point.y - p.getY())/(double)(this.point.x - p.getX());
if(this.slope == tmp){
return true;
}
return false;
}
public static void main(String[] args) {
Line l1 = new Line(new Point(1, 0), new Point(1, 3));
Line l2 = new Line(new Point(2, 0), new Point(2, 3));
System.out.println(l1.isParallel(l2));
}
}
class Point{
int x,y;
public Point(int a,int b){
this.x = a;
this.y = b;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public boolean isSamePoint(Point p){
if(this.x == p.getX() this.y == p.getY()){
return true;
}
return false;
}
}
=============================
程序执行结果为 true
分享名称:java判断平行代码 java或判断
转载来源:http://lswzjz.com/article/hhssci.html