本帖最后由 xjandrew 于 2012-12-9 23:34 编辑
首先,咱先看看这直角三角型的斜边坐标规律
x=0,y=100
x=100,y=50
x=155,y=22.5
y与x之间的关系就是
y=100-x/2
对应到代码上就是
y = yLength - x/2
所以当0<x<xLength且0<y < (yLength - x/2)时,就可以判定点在三角形内- import java.util.Scanner;
- public class Point
- {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- System.out.print("Enter a point's x- and y- coordinates: ");
- double x = input.nextDouble();
- double y = input.nextDouble();
- double xLength = 200.0;
- double yLength = 100.0;
-
- if(x >0 && x < xLength && y>0 && y < (yLength - x/2))
- System.out.println("inside");
- else
- System.out.println("outside");
- }
- }
复制代码 |