java 点是否在三角形_java – 查找点是否在三角形内

我已经在这几个小时,尝试不同的方法看几乎每个问题.也许我完全错了,但我觉得我的数学是正确的,但无论我输入什么数字,我都得到相同的输出.我的代码在某个地方关闭,我必须在午夜之前将其打开.

这一切都很有趣:找出一个点是否在三角形代码中. (对于初学者)

import java.util.Scanner;

public class PointsTriangle {

// checks if point entered is within the triangle

//given points of triangle are (0,0) (0,100) (200,0)

public static void main (String [] args) {

//obtain point (x,y) from user

System.out.print("Enter a point's x- and y-coordinates: ");

Scanner input = new Scanner(System.in);

double x = input.nextDouble();

double y = input.nextDouble();

//find area of triangle with given points

double ABC = ((0*(100-0 )+0*(0 -0)+200*(0-100))/2.0);

double PAB = ((x*(0 -100)+0*(100-y)+0 *(y- 0))/2.0);

double PBC = ((x*(100-0 )+0*(0 -y)+200*(y-100))/2.0);

double PAC = ((x*(0 -100)+0*(100-y)+200*(y- 0))/2.0);

boolean isInTriangle = PAB + PBC + PAC == ABC;

if (isInTriangle)

System.out.println("The point is in the triangle");

else

System.out.println("The point is not in the triangle");

}//end main

}//end PointsTriangle

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值