Java - Make Plot Points, Near Linear - Exactly Linear

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 20:18:24

问题


How would I go about changing points that are 'near' linear (within a threshold), actually linear?

I have some code that checks if 3 points are linear to one another (give or take), and I want to replace those coordinates with new ones that are 100% inline.

        double distance = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);

        double threshold = 4;

        if (Math.abs(distance) <= threshold) {
            // is Near line
            return true;
        }
        else
            return false;

This is an EXTENSION of another post of mine... This is NOT a repost, simply a related topic:

Java - Average Linear Graph Plots


回答1:


The technical term for snapping a point to a line is projecting a point to a line(-segment)

The only question that remains: Should the points projected to the line or to the line segment? (A line segment is only between two points, the line has infinity length and goes through both points)

The code below solves both: To allow also projecting points to th epart of the line that is outside of points A->B, the code would be much simpler, but this is covered in the link below, too.

See http://forums.codeguru.com/showthread.php?194400-Distance-between-point-and-line-segment

The projected point is in the variables (xx,yy): (xx,yy) is the point on the lineSegment closest to (cx,cy)



来源:https://stackoverflow.com/questions/13593082/java-make-plot-points-near-linear-exactly-linear

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!