Java获取某个x位置的多边形高度

Java获取某个x位置的多边形高度,java,math,polygon,Java,Math,Polygon,我正在使用libGDX。 我想得到多边形的高度,当我知道x的位置时 这张照片可以更好地解释: 我想你有一个坐标类: public class Coordinate { public double x; public double y; public Coordinate(int x, int y) { this.x = x; this.y = y; } } public class Polygon { prote

我正在使用libGDX。 我想得到多边形的高度,当我知道x的位置时

这张照片可以更好地解释:


我想你有一个坐标类:

public class Coordinate
{
    public double x;
    public double y;

    public Coordinate(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}
public class Polygon
{
    protected Coordinate[] points;

    public Polygon(Coordinate[] points)
    {
        this.points = points;
    }

    protected doulbe getMaxHeight(double x)
    {
        double height = 0;
        for (int pointIndex = 0; pointIndex < points.length; pointIndex++)
        {
            int nextIndex = (pointIndex + 1) % points.length;
            if ((points[pointIndex] >= x) && (points[nextIndex] <= x) ||
                (points[nextIndex] >= x) && (points[pointIndex] <= x))
            {
                height = Math.max(height, Math.max(points[pointIndex].y, points[nextIndex].x));
            }
        }
        return height;
    }
}
假设您有一个包含多边形坐标的类:

public class Coordinate
{
    public double x;
    public double y;

    public Coordinate(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}
public class Polygon
{
    protected Coordinate[] points;

    public Polygon(Coordinate[] points)
    {
        this.points = points;
    }

    protected doulbe getMaxHeight(double x)
    {
        double height = 0;
        for (int pointIndex = 0; pointIndex < points.length; pointIndex++)
        {
            int nextIndex = (pointIndex + 1) % points.length;
            if ((points[pointIndex] >= x) && (points[nextIndex] <= x) ||
                (points[nextIndex] >= x) && (points[pointIndex] <= x))
            {
                height = Math.max(height, Math.max(points[pointIndex].y, points[nextIndex].x));
            }
        }
        return height;
    }
}
公共类多边形
{
受保护坐标[]点;
公共多边形(坐标[]点)
{
这个点=点;
}
受保护的双重getMaxHeight(双x)
{
双倍高度=0;
对于(int pointIndex=0;pointIndex如果((点[pointIndex]>=x)和&(点[nextIndex]=x)和&(点[pointIndex]你想要哪个高度?有两个是可能的,你的图片很好地说明了这一点。其中任何一个,我想你说一个高度是从地面的0坐标开始的,另一个是从多边形的起点开始的,最大的问题是决定你想要哪个。从那里,按顺序迭代点并使用l就很容易了ine截取。直线垂直,用
x
表示,直线由两点生成。