Introduction to Java Programming编程题3.27<判断点是否在三角形内>

本文介绍了一个简单的Java程序,该程序通过输入点的坐标来判断该点是否位于一个特定的直角三角形内部。三角形的直角位于坐标原点,程序计算点到原点的距离并与三角形的最大距离进行比较。
/*
Enter a point's x- and y-coordinates: 100.5 25.5
The point is in the triangle.

Enter a point's x- and y-coordinates: 100.5 50.5
The point is not in the triangle.
*/
import java.util.Scanner;

public class PointInRightAngleTriangle {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter a point's x- and y-coordinates: ");
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        double x = 200, y = 100;
        double maxDistance = Math.sqrt(x * x + y * y) / 2;
        double distance = Math.sqrt(x1 * x1 + y1 * y1); 

        if (distance <= maxDistance) 
            System.out.println("The point is in the triangle.");
        else
            System.out.println("The point is not in the triangle.");
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值