I'm trying to figure out what is the best way to calculate the overlap area of two arbitrary polygons in Java.
Here's the research I've done so far:
I've read the documentation of Area class (from java.awt.geom). It doesn't seem to support this option.
I've tried looking at other classes that may support this, and were offered in other similar occasions (classes that implement Shape interface for example). None of them seemed to have this option.
I am aware that there are 3rd party modules that support this, but I'm looking for one with free license for every use (including commercial use).
Some more details about the Polygons:
The only assumption about the polygons is that they are "simple" - i.e - not containing any holes.
Polygons are given as a list of coordinates. I also have the Area and GeneralPath objects that represents them.
Is there any way to achieve this task in Java without downloading external libraries?
The only solution I thought of this far is to create for both a set of inner points by finding the bounding rectangle, and using Area's contain function, and then finding the union of both of these sets. The problem with this solution is that it's very inefficient.
You need polygon clipping algorithm suitable for non-convex polygons. Some known algorithms:
Vatti's algorithm, works with arbitrary polygons, including complex, used in Clipper library
Weiler-Atherton algorithm
Greiner-Hormann algorithm , page has links to some implementations
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am working on my final year computing project and need some advice/ help as I am not the most confident coder.
The project will create a software this uses an implementation of the Dijkstra’s algorithm to find the shortest path between airports. The software will allow the user to input a start airport and a destination airport within selected countries within European air space. The software will then draw the shortest path on an interactive map from waypoint to waypoint to the destination airport and additionally the route will be printed out in text will all relevant air navigation information. Additionally no fly zones can be inputted so that the path can be drawn around the no fly zone.
I am thinking of doing this in java but am confused on how to go about doing this for example, how do I get the map like google maps and then how do I add the airports and waypoints onto the graph as nodes.
As you've mentioned Google Maps I would recommend making this as a website using HTML/CSS/JavaScript. Everything you need to pull this kind of thing together already exists and it's all relatively straight forward too.
In terms of the steps you'll need to take, it will be roughly as follows:
1. Get a list of the airport information you want
Search query example: "JSON Airport information"
You'll need at least their latitude, longitude and airport code. A quick search pulled up this great looking project. Filter out the airports you don't want. Save the result as JSON, or just straight use one that's JSON already.
Add this information to your webpage by doing an ajax request - if you're new to JavaScript, most people use jQuery for that.
2. Figure out which map you want to use
You could use Google Maps, but there's so many other wonderful options like OpenLayers or anything that's based on it like MapBox. I'll be referencing OpenLayers because it's both free and a wonderfully easy project to get started with. Embed it on your site and then get to know its API. These API's are easy to understand and they provide lots of examples on how to show lines as well as markers (for the actual airports).
3. Display your airports
Using the map's API and your JSON data, put markers on the map however you'd like to display it. There's examples for flights too. The same applies to your no-fly zones; these map API's allow you to display areas too.
4. Perform your Dijkstra routing
There's a wide range of graph libraries for JavaScript. If you can't use a library for this, you can at least use them for inspiration. Use your airport information with a graph library that suits you to perform the searches you need. Finding the correct grand-circle distance can also be done with the map API, avoiding the need to implement that yourself. Use the distance as your dijkstra weights.
5. Add support for no-fly zones
This part of the question is a little vague as there's a lot of options here, as e.g. no-fly-zones have varying height and vehicle speed affects how the route should be planned. For example, it depends on if the vehicles are small drones or commercial aircraft. A simple approach is to just remove any routes which intersect your no-fly-zones; A quick search brought up this answer for help with that part. Alternatively research how routes are planned for your target vehicles and mimic that.
First question here so sorry if anything I ask is completely stupid.
I'm working in a shape recognition project where it is supposed for me to develop an application that receives two images: an original one and a sketch made by a user. I am supposed to detect contours of the two images and find the best match in the original image corresponding to the sketch made by the user.
I am already learning some basics about the Canny edge detection and was able to get the contours of several images. After having the contours, I need to analyze all contours in the image and find the best match, disregarding translation, rotation, scaling and occlusion.
Then, I found this code that does exactly what I want:
http://www.morethantechnical.com/2012/12/27/2d-curve-matching-in-opencv-w-code/ but is in C++.
Do you know any alternative for similar code in Java or any algorithm that could be useful to me? I also discovered BoofCV but it seems that such task is not implemented.
Thank you for your patience.
EDIT:
I've been searching for other ways of doing this, and I found the Hausdorff distance:
http://cgm.cs.mcgill.ca/~godfried/teaching/cg-projects/98/normand/main.html
Is it possible to modify this algorithm to be rotation invariant? They only talk about translation and scaling.
As you mentioned you already have the source code in C++ and can't find a Java version - possibly your best bet might be to convert the C++ code into Java code. If you don't need all parts of the C++ program, you might want to covert only the parts (classes) that you need.
Conversion from C++ to Java might not be always trivial but I am guessing it might be easier if you know exactly what you want and how you want your program to behave. Below is a link to some conversion tools - although they might not be free.
http://www.researchgate.net/post/How_to_convert_the_C_C_code_to_java2
I'm in the process of making a swimlane diagram but can't come up with a good algorithm to automatically lay out the lines that connect the nodes in the diagram. What I essentially want is this.
However, I don't have any protection against lines overlapping or intersecting right now and it sometimes gets very messy.
Does anyone know a way to detect if a line will intersect ANY of the lines already drawn?
One idea that I've come up with is to store the paths in an array or table and check the entire table every time a new line is slated to be drawn but that does not seem efficient.
I'm doing this in javascript and java through the use of GWT so maybe there is an easy way to solve this using one of the tools provided by these languages?
If your real issue is to minimize the line intersections, there are several algorithms that try to accomplish this in diagrams. Check this link for example, there are also more algorithms that are used in auto routing for electric design automation that are also used in this kind of diagrams, like Lee algorithm, or A* Algorithm.
I don't know if the tools that you are using have enough flexibility to implement this kind of algorithms, usually you need to implement your own heuristic according to the specific type of diagram, but I hope that this links are enough to give you good ideas.
Minimizing the line intersections in a graph is a difficult NP-Hard problem, check this link (about the crossing number) for more information.
Good luck.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
This question's answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions.
Has anyone had good experiences with any Java libraries for Graph algorithms. I've tried JGraph and found it ok, and there are a lot of different ones in google. Are there any that people are actually using successfully in production code or would recommend?
To clarify, I'm not looking for a library that produces graphs/charts, I'm looking for one that helps with Graph algorithms, eg minimum spanning tree, Kruskal's algorithm Nodes, Edges, etc. Ideally one with some good algorithms/data structures in a nice Java OO API.
If you were using JGraph, you should give a try to JGraphT which is designed for algorithms. One of its features is visualization using the JGraph library. It's still developed, but pretty stable. I analyzed the complexity of JGraphT algorithms some time ago. Some of them aren't the quickest, but if you're going to implement them on your own and need to display your graph, then it might be the best choice. I really liked using its API, when I quickly had to write an app that was working on graph and displaying it later.
Summary:
JGraphT if you are more interested in data structures and algorithms.
JGraph if your primary focus is visualization.
Jung, yWorks, and BFG are other things people tried using.
Prefuse is a no no since one has to rewrite most of it.
Google Guava if you need good datastructures only.
Apache Commons Graph. Currently dormant, but provides implementations for many algorithms. See https://issues.apache.org/jira/browse/SANDBOX-458 for a list of implemented algorithms, also compared with Jung, GraphT, Prefuse, jBPT
Check out JGraphT for a very simple and powerful Java graph library that is pretty well done and, to allay any confusion, is different than JGraph. Some sample code:
UndirectedGraph<String, DefaultEdge> g =
new SimpleGraph<String, DefaultEdge>(DefaultEdge.class);
String v1 = "v1";
String v2 = "v2";
String v3 = "v3";
String v4 = "v4";
// add the vertices
g.addVertex(v1);
g.addVertex(v2);
g.addVertex(v3);
g.addVertex(v4);
// add edges to create a circuit
g.addEdge(v1, v2);
g.addEdge(v2, v3);
g.addEdge(v3, v4);
g.addEdge(v4, v1);
JUNG is a good option for visualisation, and also has a fairly good set of available graph algorithms, including several different mechanisms for random graph creation, rewiring, etc. I've also found it to be generally fairly easy to extend and adapt where necessary.
Apache Commons offers commons-graph. Under http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/ one can inspect the source. Sample API usage is in the SVN, too. See https://issues.apache.org/jira/browse/SANDBOX-458 for a list of implemented algorithms, also compared with Jung, GraphT, Prefuse, jBPT
Google Guava if you need good datastructures only.
JGraphT is a graph library with many Algorithms implemented and having (in my oppinion) a good graph model. Helloworld Example. License: LGPL+EPL.
JUNG2 is also a BSD-licensed library with the data structure similar to JGraphT. It offers layouting algorithms, which are currently missing in JGraphT. The most recent commit is from 2010 and packages hep.aida.* are LGPL (via the colt library, which is imported by JUNG). This prevents JUNG from being used in projects under the umbrella of ASF and ESF. Maybe one should use the github fork and remove that dependency. Commit f4ca0cd is mirroring the last CVS commit. The current commits seem to remove visualization functionality. Commit d0fb491c adds a .gitignore.
Prefuse stores the graphs using a matrix structure, which is not memory efficient for sparse graphs. License: BSD
Eclipse Zest has built in graph layout algorithms, which can be used independently of SWT. See org.eclipse.zest.layouts.algorithms. The graph structure used is the one of Eclipse Draw2d, where Nodes are explicit objects and not injected via Generics (as it happens in Apache Commons Graph, JGraphT, and JUNG2).
http://neo4j.org/ is a graph database that contains many of graph algorithms and scales better than most in-memory libraries.
In a university project I toyed around with yFiles by yWorks and found it had pretty good API.
check out Blueprints:
Blueprints is a collection of interfaces, implementations, ouplementations, and test suites for the property graph data model. Blueprints is analogous to the JDBC, but for graph databases. Within the TinkerPop open source software stack, Blueprints serves as the foundational technology for:
Pipes: A lazy, data flow framework
Gremlin: A graph traversal language
Frames: An object-to-graph mapper
Furnace: A graph algorithms package
Rexster: A graph server
http://incubator.apache.org/hama/ is a distributed scientific package on Hadoop for massive matrix and graph data.
JDSL (Data Structures Library in Java) should be good enough if you're into graph algorithms - http://www.cs.brown.edu/cgc/jdsl/
For visualization our group had some success with prefuse. We extended it to handle architectural floorplates and bubble diagraming, and it didn't complain too much. They have a new Flex toolkit out too called Flare that uses a very similar API.
UPDATE:
I'd have to agree with the comment, we ended up writing a lot of custom functionality/working around prefuse limitations. I can't say that starting from scratch would have been better though as we were able to demonstrate progress from day 1 by using prefuse. On the other hand if we were doing a second implementation of the same stuff, I might skip prefuse since we'd understand the requirements a lot better.
Try Annas its an open source graph package which is easy to get to grips with
http://annas.googlecode.com
It's also good to be convinced that a Graph can be represented as simply as :
class Node {
int value;
List<Node> adj;
}
and implement most the algorithms you find interesting by yourself. If you fall on this question in the middle of some practice/learning session on graphs, that's the best lib to consider. ;)
You can also prefer adjacency matrix for most common algorithms :
class SparseGraph {
int[] nodeValues;
List<Integer>[] edges;
}
or a matrix for some operations :
class DenseGraph {
int[] nodeValues;
int[][] edges;
}
I don't know if I'd call it production-ready, but there's jGABL.
If you need performance, you might take a look at Grph. The library is developed in the French University and CNRS/Inria.
http://www.i3s.unice.fr/~hogie/grph/
The project is active and reactive support is provided!
Instructional graph algorithm implementations in java could be found here (by prof. Sedgewick et al.):
http://algs4.cs.princeton.edu/code/
I was introduced to them while attending these exceptional algorithm courses on coursera (also taught by prof. Sedgewick):
https://www.coursera.org/course/algs4partI
https://www.coursera.org/course/algs4partII
If you are actually looking for Charting libraries and not for Node/Edge Graph libraries I would suggest splurging on Big Faceless Graph library (BFG). It's way easier to use than JFreeChart, looks nicer, runs faster, has more output options, really no comparison.
JGraph from http://mmengineer.blogspot.com/2009/10/java-graph-floyd-class.html
Provides a powerfull software to work with graphs (direct or undirect). Also generates Graphivz code, you can see graphics representations. You can put your own code algorithms into pakage, for example: backtracking code. The package provide some algorithms: Dijkstra, backtracking minimun path cost, ect..