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));
}
}
}
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
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.
Image showing results
And my chart looks like this:
Image for Jfreechart
Please suggest the problem with this.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…