系列的交点 [英] Intersection points of series

查看:53
本文介绍了系列的交点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在JFreeChart中找到两个系列的交点?相交的图表系列之间没有任何共同点.因此,需要为图上恰好相交的两个序列计算交点.

Is it possible to find the intersection points of two series in JFreeChart? The chart series that are intersecting don't have any common points among them. So, the intersection point needs to be calculated for two series that happens to intersect each other on the graph.

List<Line> lineOne = one.getItems(); 
List<Line> lineTwo = two.getItems(); 
for (Line i : lineOne) { 
    for (Line j : lineTwo) { 
        if (i.intersection(j) != null) { 
            System.out.println(i.intersection(j)); 
        } 
    } 
}

上面的代码是我想做的,但是此消息抛出 ClassCastException :

The code above is what I was trying to do, but this throws a ClassCastException with this message:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
org.jfree.data.xy.XYDataItem cannot be cast to
org.apache.commons.math3.geometry.euclidean.twod.Line

在Trashgod的建议下,我尝试执行以下操作:

After Trashgod's suggestion I have tried doing the following:

List<XYDataItem> l1 = one.getItems();
List<XYDataItem> l2 = two.getItems();

Line itemOne = null;
Line itemTwo = null;
List<Line> lineOne = new ArrayList<Line>();
List<Line> lineTwo = new ArrayList<Line>();

//Add lines to the first list
for(int i = 0; i < l1.size(); i++){
    if(i < l1.size()-1) {
        itemOne = new Line(new Vector2D(l1.get(i).getXValue(),
                        l1.get(i).getYValue()), 
                        new Vector2D(l1.get(i+1).getXValue(), 
                        l1.get(i+1).getYValue()), 0);
        lineOne.add(itemOne);
    }
}

//Add lines to the second list
for(int i = 0; i < l2.size(); i++){
    if(i < l2.size()-1) {
        itemTwo = new Line(new Vector2D(l2.get(i).getXValue(),
                        l2.get(i).getYValue()), 
                        new Vector2D(l2.get(i+1).getXValue(), 
                        l2.get(i+1).getYValue()), 0);
        lineTwo.add(itemTwo);
    }
}

for(Line i: lineOne) {
    for(Line j: lineTwo) {
        if (i.intersection(j) != null) { 
            System.out.println(i.intersection(j)); 
        }                       
    }
}

但是,即使只有很少的交点,在遍历此列表时,它也会产生很多结果(如下图所示).
而且交点也不正确.

However, while traversing through this list, it yields a lot of results (as shown in the image below) even if there are only few intersection points.
And the intersection points are not correct either.

显示结果的图像

我的图表如下所示: Jfreechart的图像

请提出与此有关的问题.

Please suggest the problem with this.

推荐答案

是的,您可以迭代包含 XYSeries 内部具有 XYDataItem List .因为 XYDataItem 实现了 Comparable ,所以您可以使用 equals()测试交集.给定两个包含一个共同项的系列,

Yes, you can iterate though the items comprising the Series. As a concrete example, an XYSeries has a List of XYDataItem internally. Because XYDataItem implements Comparable, you can test for intersection using equals(). Given two series containing a single item in common,

private final XYSeries one = new XYSeries("One");
private final XYSeries two = new XYSeries("Two");
…
one.add(1, 1);
one.add(1, 42);
two.add(1, 2);
two.add(1, 42);

以下迭代方案找到公共点 [1.0,42.0] :

The following iteration scheme finds the common point, [1.0, 42.0]:

List<XYDataItem> list1 = one.getItems();
List<XYDataItem> list2 = two.getItems();
for (XYDataItem i : list1) {
    for (XYDataItem j : list2) {
        if (i.equals(j)) {
            System.out.println(i);
        }
    }
}

或者,您可以使用功能性操作:

Alternatively, you can use functional operations:

list1.stream().forEach((i) -> {
    list2.stream().filter((j) -> (i.equals(j))).forEach((item) -> {
        System.out.println(i);
    });
});

我需要找到两个 XYLineChart 系列的交点,它们之间没有共同的数据点.

I need to find the intersection of two XYLineChart series which don't have common data points among them.

您可以在 List< Line> 的两个实例上使用相同的迭代方案;每个列表应包含连接相应系列的连续点的线.用概念性的 intersects()方法替换 equals().您可以使用 线与线的交点 ;

You can use the same iteration scheme on two instances of List<Line>; each list should contain the lines connecting consecutive points of the corresponding series. Replace equals() with a notional intersects() method. You can use line–line intersection; Line::intersection is a typical implementation.

上面的代码…引发 ClassCastException 和消息,

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
org.jfree.data.xy.XYDataItem cannot be cast to
org.apache.commons.math3.geometry.euclidean.twod.Line

那是预料之中的;您必须遍历每个 List< XYDataItem> 来创建相应的 List< Line> .第一个 Line 将由点1和2界定;第二条 Line 指向第2点和第3点,依此类推.

That is expected; you'll have to traverse each List<XYDataItem> to create the corresponding List<Line>. The first Line will be bounded by points 1 and 2; the second Line by points 2 and 3, etc.

这篇关于系列的交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆