【问题标题】:Calculate curvature for 3 Points (x,y)计算 3 点 (x,y) 的曲率
【发布时间】:2016-12-14 13:47:34
【问题描述】:

我有一个二维欧几里得空间。给出三分。

例如(p2为中点):

Point2D p1 = new Point2D.Double(177, 289);
Point2D p2 = new Point2D.Double(178, 290);
Point2D p3 = new Point2D.Double(178, 291);

现在我想计算这三个点的curvature

double curvature = calculateCurvature(p1, p2, p3);

如何做到这一点? 有没有现成的方法(没有java外部库)?

【问题讨论】:

  • 请添加更多关于您尝试过的内容或代码中无效的信息

标签: java point curve


【解决方案1】:

对于门格尔曲率,公式在维基百科文章中是正确的there

curvature = 4*triangleArea/(sideLength1*sideLength2*sideLength3)

您具体尝试了哪个代码?

考虑到你的 3 分,计算这 4 个值应该不会太难。

Here 是一些有用的方法:

/**
 * Returns twice the signed area of the triangle a-b-c.
 * @param a first point
 * @param b second point
 * @param c third point
 * @return twice the signed area of the triangle a-b-c
 */
public static double area2(Point2D a, Point2D b, Point2D c) {
    return (b.x-a.x)*(c.y-a.y) - (b.y-a.y)*(c.x-a.x);
}

/**
 * Returns the Euclidean distance between this point and that point.
 * @param that the other point
 * @return the Euclidean distance between this point and that point
 */
public double distanceTo(Point2D that) {
    double dx = this.x - that.x;
    double dy = this.y - that.y;
    return Math.sqrt(dx*dx + dy*dy);
}

没有更多的事情要做。警告:area2 返回一个带符号的双精度数,具体取决于您的点的方向(顺时针或逆时针)。

【讨论】:

  • 不知道Point2D是什么,但是Java的Point2D没有这些方法:docs.oracle.com/javase/7/docs/api/java/awt/geom/Point2D.html
  • 所以定义它们,并将b.x 替换为b.getX() ;)
  • 我即将这样做。谢谢你:)
  • 我还想指出,Heron 公式是获得三角形(无符号)面积的数值稳定方法。事实上,它自然地给你 4* 面积,它的输入是边的长度——这正是你已经需要的门格曲率的恶魔。
【解决方案2】:

正如Eric Duminil in his answer 已经指出的那样,计算是

curvature = 4*triangleArea/(sideLength0*sideLength1*sideLength2)

我浪费了一些时间来创建这个包含computeCurvature 方法的交互式示例,该方法可以一次完成整个计算:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class CurvatureFromThreePoints
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
    
    private static void createAndShowGUI()
    {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new CurvatureFromThreePointsPanel());
        f.setSize(800,800);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

}

class CurvatureFromThreePointsPanel extends JPanel 
    implements MouseListener, MouseMotionListener
{
    private final List<Point2D> pointList;
    private Point2D draggedPoint;
    
    public CurvatureFromThreePointsPanel()
    {
        this.pointList = new ArrayList<Point2D>();
        
        pointList.add(new Point2D.Double(132,532));
        pointList.add(new Point2D.Double(275,258));
        pointList.add(new Point2D.Double(395,267));

        addMouseListener(this);
        addMouseMotionListener(this);
    }
    
    private static double computeCurvature(Point2D p0, Point2D p1, Point2D p2)
    {
        double dx1 = p1.getX() - p0.getX();
        double dy1 = p1.getY() - p0.getY();
        double dx2 = p2.getX() - p0.getX();
        double dy2 = p2.getY() - p0.getY();
        double area = 0.5 * (dx1 * dy2 - dy1 * dx2;
        double len0 = p0.distance(p1);
        double len1 = p1.distance(p2);
        double len2 = p2.distance(p0);
        return 4 * area / (len0 * len1 * len2);
    }
    
    // Adapted from https://stackoverflow.com/a/4103418
    private static Point2D computeCircleCenter(
        Point2D p0, Point2D p1, Point2D p2)
    {
        double x0 = p0.getX();
        double y0 = p0.getY();
        double x1 = p1.getX();
        double y1 = p1.getY();
        double x2 = p2.getX();
        double y2 = p2.getY();
        double offset = x1 * x1 + y1 * y1;
        double bc = (x0 * x0 + y0 * y0 - offset) / 2.0;
        double cd = (offset - x2 * x2 - y2 * y2) / 2.0;
        double det = (x0 - x1) * (y1 - y2) - (x1 - x2) * (y0 - y1);
        double invDet = 1 / det;
        double cx = (bc * (y1 - y2) - cd * (y0 - y1)) * invDet;
        double cy = (cd * (x0 - x1) - bc * (x1 - x2)) * invDet;
        return new Point2D.Double(cx, cy);
    }
    
    @Override
    protected void paintComponent(Graphics gr)
    {
        super.paintComponent(gr);
        Graphics2D g = (Graphics2D)gr;
        
        g.setColor(Color.RED);
        for (Point2D p : pointList)
        {
            double r = 5;
            g.draw(new Ellipse2D.Double(p.getX()-r, p.getY()-r, r+r, r+r));
        }
        
        g.setColor(Color.BLACK);
        //g.draw(Paths.fromPoints(spline.getInterpolatedPoints(), false));
        
        Point2D p0 = pointList.get(0);
        Point2D p1 = pointList.get(1);
        Point2D p2 = pointList.get(2);
        double curvature = computeCurvature(p0, p1, p2);
        g.drawString("Curvature: "+curvature, 10,  20);
        
        Point2D center = computeCircleCenter(p0, p1, p2);
        double radius = center.distance(p0);
        g.draw(new Ellipse2D.Double(
            center.getX() - radius, center.getY() - radius,
            radius + radius, radius + radius));
    }
    
    @Override
    public void mouseDragged(MouseEvent e)
    {
        if (draggedPoint != null)
        {
            draggedPoint.setLocation(e.getX(), e.getY());
            repaint();
            
            System.out.println("Points: ");
            for (Point2D p : pointList)
            {
                System.out.println("    "+p);
            }
        }
    }


    @Override
    public void mousePressed(MouseEvent e)
    {
        final double thresholdSquared = 10 * 10;
        Point2D p = e.getPoint();
        Point2D closestPoint = null;
        double minDistanceSquared = Double.MAX_VALUE;
        for (Point2D point : pointList)
        {
            double dd = point.distanceSq(p);
            if (dd < thresholdSquared && dd < minDistanceSquared)
            {
                minDistanceSquared = dd;
                closestPoint = point;
            }
        }
        draggedPoint = closestPoint;
    }

    @Override
    public void mouseReleased(MouseEvent e)
    {
        draggedPoint = null;
    }

    @Override
    public void mouseMoved(MouseEvent e)
    {
        // Nothing to do here
    }


    @Override
    public void mouseClicked(MouseEvent e)
    {
        // Nothing to do here
    }

    @Override
    public void mouseEntered(MouseEvent e)
    {
        // Nothing to do here
    }


    @Override
    public void mouseExited(MouseEvent e)
    {
        // Nothing to do here
    }


}

【讨论】:

  • 令人印象深刻的作品。 @Spen:这个人应该得到公认的答案,他应得的!
  • 次要 nitpick : sideLength0 不应该出现三次,对吗?
  • 最后,由于曲率取决于比例,在屏幕上显示单位长度可能是个好主意。但又一次:令人印象深刻且有趣的工作!
  • @EricDuminil 我的大部分答案都只是围绕您已经写的内容而闪光,而您的答案在这个意义上是“更重要的”(除了实际的computeCurvature 函数,它仍然具有从您的 sn-ps 组装 - 如果您愿意,可以将其添加到您的答案中,因为大多数人会首先查看已接受的答案)。关于错别字:我现在就改正。
  • computeCurvature 似乎返回两倍的曲率,因为 area 公式计算的是三角形面积的 2 倍。看起来 4 应该在 return 语句中更改为 2
【解决方案3】:

从您引用的wiki 中,曲率定义为

其中 A 是由三个点 x、y 和 z(在您的情况下为 p1、p2、p3)和 |x-y| 形成的三角形所包围的区域。是点 x 和 y 之间的距离。

将公式翻译成代码就完成了!

【讨论】:

    【解决方案4】:

    C/C++

    // https://www.mathopenref.com/coordtrianglearea.html
    float getAreaOfTriangle(Point2f A, Point2f B, Point2f C)
    {
        return fabs(
                (A.x * (B.y - C.y) + B.x * (C.y - A.y) + C.x * (A.y - B.y)) / 2);
    }
    
    float getDistFromPtToPt(Point2f pt1, Point2f pt2)
    {
        return sqrt((pt2.x - pt1.x) * (pt2.x - pt1.x) +
                    (pt2.y - pt1.y) * (pt2.y - pt1.y));
    }
    
    
    // https://en.wikipedia.org/wiki/Menger_curvature
    float
    getCurvatureUsingTriangle(Point2f pt1, Point2f pt2, Point2f pt3, bool bDebug)
    {
        float fAreaOfTriangle = getAreaOfTriangle(pt1, pt2, pt3);
        float fDist12 = getDistFromPtToPt(pt1, pt2);
        float fDist23 = getDistFromPtToPt(pt2, pt3);
        float fDist13 = getDistFromPtToPt(pt1, pt3);
        float fKappa = 4 * fAreaOfTriangle / (fDist12 * fDist23 * fDist13);
        return fKappa;
    }
    

    【讨论】:

      猜你喜欢
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-27
      相关资源
      最近更新 更多