java 点是否在三角形_java – 查找点是否在三角形内
我已經(jīng)在這幾個(gè)小時(shí),嘗試不同的方法看幾乎每個(gè)問題.也許我完全錯(cuò)了,但我覺得我的數(shù)學(xué)是正確的,但無論我輸入什么數(shù)字,我都得到相同的輸出.我的代碼在某個(gè)地方關(guān)閉,我必須在午夜之前將其打開.
這一切都很有趣:找出一個(gè)點(diǎn)是否在三角形代碼中. (對(duì)于初學(xué)者)
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
總結(jié)
以上是生活随笔為你收集整理的java 点是否在三角形_java – 查找点是否在三角形内的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 扩展数据类型_java数据类型
- 下一篇: java主界面设置背景图片_java 窗