“Group” vecmath Point objects based on the distance between them - java

I have a load of vecmath Point objects (Point3d FWIM) which I would like to “group” based on the distance between them. I can probably write the code for this from scratch (I’ve done similar tasks in excel), but I like the idea of using existing libraries where possible. The problem is I can’t find any libraries like this.
I haven’t thought through the exact algorithm fully, but I hope I’ve done enough for the question not to be deleted. Please bear with me, I'm still new here at the time of this post.
I imagine the grouping would work as follows:
decide the distanceLimit
loop 1: for each Point, calculate the distance to each other Point
Make a "Set"
loop 2: for each Point
if the next Point is within the distanceLimit of any previously considered Points up to i, add it to current "Set"
Else make a new "Set".
Edit: ah the power of verbalising one's ideas. The above doesn't the capture the situation where points 1 and 2 are between one and two distanceLimits apart and initiate separate "sets", and point 3 crops up halfway between them meaning that all three should be in one set really. Need to think about this some more!
I’m also not sure yet what data structures I should really use for the input and output (ArrayLists? Sets?).
Ideally I am looking for an existing library that does this or similar; if you’re confident there isn’t one, then any suggestions for the algorithm or the actual code would be more than welcome.

After lots more googling, I found that:
what I was trying to do is called clustering;
this did exactly what I was trying to do; I was impressed with how well it worked for me.

Related

Kinect 2 Object tracking best method

What is the best method to track/recognize an object using a Kinect and Java or C programming after having a constant track on the object in 3D space I wanted to have the coordinates.
I know the exact object I wanna tack and wanted to the most convenient way to track the object.
I've currently programming with processing using Java, I'm a newbie to this any help would be appreciated.
Stack Overflow isn't really designed for general "how do I do this" type questions. It's designed for more specific "I tried X, expected Y, but got Z instead" type questions. That being said, I'll try to help in a general sense:
Break your problem down into smaller pieces.
Step 1: Can you get Kinect data feeding into your code? Don't worry about doing anything with the data, just display it on the screen for now. Googling something like "Processing Kinect" returns a ton of results, or you could check out the Processing libraries page.
Step 2: After you get that working, then can you identify your target point? Then can you track that point? Again, google is your friend. You might also consider treating this as a separate problem and using something like OpenCV to do image processing on the Kinect feed.
Open Kinect by Daniel Shiffman is a pretty good starting point, and it contains a bunch of examples that get you closer to your goal.
That should be a reasonable starting point: break your problem down into smaller steps, then use google searches to approach those steps one at a time. If you get stuck on a specific step, come back and ask a specific question (don't forget the MCVE) and we'll go from there. Good luck.

Dealing with recursive methods [duplicate]

This question already has answers here:
Understanding recursion [closed]
(20 answers)
Closed 7 years ago.
When you use recursion to solve a problem, why does the recursion method need to call itself to solve a smaller version of the original problem?
By definition, a recursive function calls itself.
If you are asking when or why you would use recursion, it is well suited for problems that resemble a fractal. IOW, when small parts of the problem look similar to larger versions of the problem.
The general idea behind solving a problem recursively is to find a way to break the problem down into smaller copies of itself, solve those smaller copies, then combine the results together to solve the overall problem.
So let's suppose that you're trying to write a function solveProblemX that solves a problem X recursively. You break the problem down into smaller copies of problem X, and now you need to solve each of them. How do you do that? Well, you're currently writing a method called solveProblemX that specifically is designed to solve problem X, so one option would be to recursively call solveProblemX on those smaller problem instances. After all - if solveProblemX really is supposed to solve problems of type X, there's nothing wrong with doing this!
What changes from problem to problem is how you break the problem down into smaller copies of itself and how you combine them back together. In each case, though, just keep in mind that the function you're writing is designed solve problems of one type, so there's nothing concerning about calling the method you're writing to solve smaller problems of that type.
I'll try to explain the way that I had it explained to me.
Say you're in a line at a coffee shop. The line is really long, and you're all the way at the back. You want to know how many people are in front of you, but you can't see the front, so you can't count everyone yourself. You instead ask the person in front of you "How many people are in front of you?". He then follows your same logic, and asks the person in front of him. Everyone asks the person in front of them until they get to the person at the register. Once the person in front is asked, they answer with 0. That would be the base case. After the person in front responds, the second person in line responds with 0+1, so 1. That continues all the way back to you until you know how many people are in line.
I'm paraphrasing something from somewhere, but I don't remember where I originally saw it.

Idea for Creating a Program to Solve a Word Prob

So I am working on a word problem, and it goes something like this:
! section removed
I am not posting the exact question or diagram just in case someone cannot stop themselves from posting a direct answer.
I am trying to write a program in Java for this, but I can't really get a grip on how to do this logically. I know I can start of with an array for each square like this:
int square1 = //four sides with each liquid value
but I do not know how to include the unlimited ones (which are diagonal), or how to continue once I figure it out.
Any help with this programming logic is appreciated.
I'd say go through each pipe and record the maximum going through it, which should be the lower of its capacity or the sum of the maxima going into it. The unlimited ones then just take the total going into them (or, at the very beginning, Integer.MAX_VALUE). Or you could use a bool for tracking whether it's infinite. Or, just for fun, Double.POSITIVE_INFINITY.
Note that if there are any cycles in the pipes, you may need to cycle through and update a couple times, until you get a steady answer.
Edit: After looking a bit at the max flow problem linked in a comment and thinking about it, I'm not so sure this is the right way to go, as I don't think it accounts for splitting current between two outgoing pipes. Perhaps some distant relative of Ohm's law, adapted for water?

Java library for creating straight skeleton?

I have as an input a 2D polygon with holes, and I need to find it's straight skeleton, like in the picture:
(source: cgal.org)
Maybe there is a good Java library for it?
And if not, can you point me to the good explanation of the algorithm, so I could implement it myself? (I haven't found good resources on Google)
I wrote this a little while back. Not sure if it's robust enough.
https://github.com/twak/campskeleton
(edited for 2018...)
See http://www.sable.mcgill.ca/~dbelan2/roofs/roofs.html which contains an applet.
You may be able to use the JTS Topology Suite. It is a very capable library that I've used on a number of projects - never for straight skeleton, but it may be possible.
Edit:
Ah. I see that "Straight Skeleton" is a technical term. The wikipedia article references several algorithms. Have you looked at those?
As I understand it, you have a (convex?) polygon. From it, you subtract 1 or more (potentially non-convex) polygons. You want to turn the result into a set of polygons without holes. Are there extra rules that you're trying to apply?
I have a hard time coming up with a set of rules from the example that you provided. The outer polygons are non-convex; so it doesn't seem like you're trying to find a convex set to represent the result (which is a relatively common task).
If you could use the breakdown shown below, the algorithm is pretty simple. Can you clarify?
Can I ask u what is your purpose for finding Straight skeleton? Is it personal or commercial? I would be interested in knowing how you r using it to solve real time problems? I do have a java library that does that. My algorithm is listed here http://web.stcloudstate.edu/rsarnath/skeleton/definition.htm

The King's Maze

I'm practicing up for a programming competition, and i'm going over some difficult problems that I wasn't able to answer in the past. One of them was the King's Maze. Essentially you are given an NxN array of numbers -50<x<50 that represent "tokens". You have to start at position 1,1(i assume that's 0,0 in array indexes) and finish at N,N. You have to pick up tokens on cells you visit, and you can't step on a cell with no token (represented by a 0). If you get surrounded by 0's you lose. If there is no solution to a maze you output "No solution". Else, you output the highest possible number you can get from adding up the tokens you pick up.
I have no idea how to solve this problem. I figured you could write a maze algorithm to solve it, but that takes time, and in programming contests you are only given two hours to solve multiple problems. I'm guessing there's some sort of pattern i'm missing. Anyone know how I should approach this?
Also, it might help to mention that this problem is meant for high school students.
This type of problem is typically solved using dynamic programming or memoization.
Basically you formulate a recursive solution, and solve it bottom up while remembering and reusing previously computed results.
The simple approach (i.e. simplest to code) is try all the possible paths - try each first step; for each first step try each second step; for each first step/second step combination try each third step; and so on. However depending on how big the maze is this may take too long to run (or it may not).
Your next step is to think about how you can do this faster. The first step is usually to eliminate moves that you know can't lead to a finish, or can't lead to a finish with higher points than the one you already have. Since this is practice for a competition we'll leave you to do this work yourself.
Think "graph" algorithms: The Algorithm Design Manual

Categories

Resources