代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
java判断点在三角形内
作者:
周明瑞
/ 发布于
2013/2/1
/
639
package com.mingrui.soft_struct; import java.awt.geom.Line2D; public class Triangle2D{ private MyPoint p0; private MyPoint p1; private MyPoint p2; Triangle2D(){ this(0.0,0.0,1.0,1.0,2.0,5.0); } Triangle2D(double x,double y,double x1,double y1,double x2,double y2){ p0=new MyPoint(x,y); p1=new MyPoint(x1,y1); p2=new MyPoint(x2,y2); } public MyPoint getP0(){ return p0; } public MyPoint getP1(){ return p1; } public MyPoint getP2(){ return p2; } public double getArea(){ double p=getPerimeter()/2.0; return Math.sqrt(p* (p-MyPoint.distance(p0,p1))* (p-MyPoint.distance(p1,p2))* (p-MyPoint.distance(p0,p2))); } public double getPerimeter(){ return MyPoint.distance(p0,p1)+MyPoint.distance(p1,p2)+MyPoint.distance(p2,p0); } public int contains(MyPoint p){ /* 0. in the line 1.is a point 2.in the triangle 3.out of the triangle */ Line2D.Double ld = new Line2D.Double(p0.getX(), p0.getY(), p1.getX(), p1.getY()); Line2D.Double ld1 = new Line2D.Double(p1.getX(), p1.getY(), p2.getX(), p2.getY()); Line2D.Double ld2 = new Line2D.Double(p0.getX(), p0.getY(), p2.getX(), p2.getY()); Line2D.Double ld3 = new Line2D.Double(p0.getX(), p0.getY(), p.getX(), p.getY()); Line2D.Double ld4 = new Line2D.Double(p1.getX(), p1.getY(), p.getX(), p.getY()); Line2D.Double ld5 = new Line2D.Double(p2.getX(), p2.getY(), p.getX(), p.getY()); if(ld.contains(p.getX(), p.getY())||ld1.contains(p.getX(), p.getY())||ld2.contains(p.getX(), p.getY())) return 0; if(LineUtil.isAPoint(p0, p1, p2, p)) return 1; if(ld.intersectsLine(ld5)) return 3; else if(ld1.intersectsLine(ld3)) return 3; else if(ld2.intersectsLine(ld4)) return 3; else if(ld.ptLineDist(p.getX(), p.getY())<ld.ptLineDist(p2.getX(),p2.getY()) &&ld1.ptLineDist(p.getX(), p.getY())<ld1.ptLineDist(p0.getX(),p0.getY()) &&ld2.ptLineDist(p.getX(), p.getY())<ld2.ptLineDist(p1.getX(),p1.getY())) return 2; else return 3; } public void overlaps(Triangle2D t){ int i=contains(t.getP0()); int j=contains(t.getP1()); int k=contains(t.getP2()); /* * 0. in the line * 1.is a point * 2.in the triangle * 3.out of the triangle */ if(i==3&&j==3&&k==3){ System.out.println("out of the triangle"); } else System.out.println("in the triangle"); //return false; } public static void main(String args[]){ Triangle2D t1=new Triangle2D(); Triangle2D t2=new Triangle2D(); MyPoint p=new MyPoint(1,1.3); System.out.println(t1.contains(p)); t1.overlaps(t2); } } class MyPoint{ private double x; private double y; MyPoint(){ this.x=0; this.y=0; } MyPoint(double x,double y){ this.x=x; this.y=y; } public double getX(){ return x; } public double getY(){ return y; } public static double distance(MyPoint p,MyPoint p1){ return Math.sqrt((p.getX()-p1.getX())*(p.getX()-p1.getX())+(p.getY()-p1.getY())*(p.getY()-p1.getY())); } public static double distance(int x,int y,int x1,int y1){ return Math.sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1)); } } class LineUtil{ public static double getK(MyPoint p0,MyPoint p1){ return (p0.getY()-p1.getY())/(p0.getX()-p1.getX()); } public static double getK(MyPoint p0,double x,double y){ return (p0.getY()-y)/(p0.getX()-x); } public static boolean isAPoint(MyPoint p0,MyPoint p1,MyPoint p2,MyPoint thePointToTest){ if((p0.getX()==thePointToTest.getX()&&p0.getY()==thePointToTest.getY()) ||(p1.getX()==thePointToTest.getX()&&p1.getY()==thePointToTest.getY()) ||(p2.getX()==thePointToTest.getX()&&p2.getY()==thePointToTest.getY())){ return true; } else return false; } }
试试其它关键字
点在三角形内
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
周明瑞
贡献的其它代码
(
3
)
.
Java解决迷宫问题
.
大文件的分割与合并
.
java判断点在三角形内
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3