「java线性斜率」线性相关斜率
今天给各位分享java线性斜率的知识,其中也会对线性相关斜率进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
用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直线求斜率的时候为什么要求公约数
因为例如up=4,down=2,和up=8,down=4,计算出来的斜率是一样的,这俩种情况的取值可能会出现在一条直线上。返回值则是a和b的最大公约数,所以JAVA直线求斜率的时候要求公约数。
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求一条直线的斜率
k
=
tan
a
//斜率
tan
a
=
(y2-y1)/(x2-x1)//a为度数,(x1,y1),(x2,y2)分别为直线2个点的坐标
public
class
Test
{
public
Double
k;
//斜率
//计算斜率
public
Double
countK(Double
x1,Double
y1,Double
x2,Double
y2){
return
(y2-y1)/(x2-x1);
}
public
static
void
main(String[]
args)
{
System.out.println(new
Test().countK(3.0,
4.0,
5.0,
6.0));
}
}
java线性斜率的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于线性相关斜率、java线性斜率的信息别忘了在本站进行查找喔。
发布于:2022-12-26,除非注明,否则均为
原创文章,转载请注明出处。