zoukankan      html  css  js  c++  java
  • 判断一个点是否在三个点组成的三角形内 java 代码 面试经典

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;

    class Point {
        
    int x;
        
    int y;

        Point(
    int x, int y) {
            
    this.x = x;
            
    this.y = y;
        }
    }

    public class pointInTrangle {
        
    public static void main(String[] args) {
            Point p1 
    = null;
            Point p2 
    = null;
            Point p3 
    = null;
            Point p 
    = null;
            System.out.println(
    "请输入四个点");
            System.out.println(
    "第一个点");
            p1 
    = input();
            System.out.println(
    "第二个点");
            p2 
    = input();
            System.out.println(
    "第三个点");
            p3 
    = input();
            System.out.println(
    "第四个点");
            p 
    = input();
            System.out.println(panduan(p1, p2, p3, p) 
    ? "在三角形内" : "不在三角形内");
        }

        
    public static boolean panduan(Point a, Point b, Point c, Point p) {
            
    double abc = triangleArea(a, b, c);
            
    double abp = triangleArea(a, b, p);
            
    double acp = triangleArea(a, c, p);
            
    double bcp = triangleArea(b, c, p);
            
    if (abc == abp + acp + bcp) {
                
    return true;
            } 
    else {
                
    return false;
            }
        }

        
    private static double triangleArea(Point a, Point b, Point c) {// 返回三个点组成三角形的面积
            double result = Math.abs((a.x * b.y + b.x * c.y + c.x * a.y - b.x * a.y
                    
    - c.x * b.y - a.x * c.y) / 2.0D);
            
    return result;
        }

        
    private static Point input() {
            BufferedReader br 
    = new BufferedReader(new InputStreamReader(System.in));
            
    int a = 0, b = 0;
            
    try {
                a 
    = Integer.parseInt(br.readLine());
                b 
    = Integer.parseInt(br.readLine());
            } 
    catch (NumberFormatException e) {
                e.printStackTrace();
            } 
    catch (IOException e) {
                e.printStackTrace();
            }
            
    return new Point(a, b);
        }
    }

  • 相关阅读:
    RFID亮灯电子标签在仓储管理中的应用
    漫画:寻找股票买入卖出的最佳时机(动态规划)
    JAVA深入解析-36个话题-Two
    一行代码让训练速度提升2倍,飞桨自动混合精度技术详解
    追源码的平凡之路
    看完这篇,你也是字符编码大神!
    微服务的熔断原理与实现
    经典论文复现 | PyraNet:基于特征金字塔网络的人体姿态估计
    一文看懂人机对话
    「Spring Boot 2.4 新特性」一键构建Docker镜像
  • 原文地址:https://www.cnblogs.com/zhonghan/p/1446730.html
Copyright © 2011-2022 走看看