Algorithmic question: Best angle to view trees from fixed camera [closed] - algorithm

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 1 year ago.
Improve this question
I was asked this question in an interview, have no clue how to solve it.
"Given a fixed camera in a forest (with predefined trees), give the best angle in which the camera pictures the maximum of trees"
How would you approach it or at least what questions would you ask to get more requirements?

If trees don't obscure over trees then:
Sort all trees by angle around the camera position.
Use sliding window approach to find direction to look at.
If trees can obscure other trees then the second step is a bit trickier.

the idea is this:
convert the list of tree coordinates to a list of angles.
sort the list of angles
use a sliding window to find the starting and ending indices that maximize the number of trees.
note: because the best angle to position the camera might actually be very near the 360 degree, you need to take into account trees on the other side of the 360/0 line. The easiest way to handle that is to add duplicate trees to the list (in step 2) with a 360 shift. for example, a tree in degree 10 would be added twice, at degree 10 and 360+10. you don't actually need to add ALL the trees twice - you only really need to duplicate trees in the range 360+camera_angle, but its easy to just duplicate all the trees and it doesn't hurt.

Related

A* Heuristic implementation [closed]

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 5 years ago.
Improve this question
I am creating a program to solve this puzzle in the lowest cost
The sliding-title puzzle consists of three black titles, three white titles, and an empty space in the configuration shown in the Figure.
WWW_BBB
The goal of the puzzle is to get all white tiles to the right of the
black tiles while the location of the space does not matter
The puzzle has two legal moves(i.e. actions) with associated costs:
• A title may move into an adjacent empty location. – This has a step cost of
• A title can hop over one or two other tiles into the empty position.
– This has a step cost equal to the number of tiles jumped over.
I am having troubles understand how to create a Heuristic algorithm to be implemented in the algorithm.
I understand the implementation of Dijkstra's algorithm within this problem, but can't figure out how to then make it into the A* algorithm.
Assuming you want to use A* on the graph of puzzle states with edges to the states reachable through one of the two rules, then a good heuristic to use would be the number of inversions: https://en.wikipedia.org/wiki/Inversion_(discrete_mathematics)
That's the number of W,B pairs that are out of order (assuming that the relative order of same-colored tiles doesn't change). The W's and B's are in order when the number of versions is 0, and the number of inversions fixed by each type of move is less than or equal to its cost. Therefore the number of inversions as a heuristic will never overestimate the cost of the best sequence.

How to find the largest circle that lies within boundaries of Polygon with genetic algorithm? [closed]

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 7 years ago.
Improve this question
I want to implement code that finds the largest circle that lies within boundaries of polygon with genetic algorithm.
Does anyone ha any idea?
There probably is a known algorithm in computational geometry for doing this exactly. If you want to do it using a genetic algorithm and are satisifed with a good circle rather than an optimal circle, then that is certainly possible (although an evolutionary algorithm seems a bit more natural). Circles can be represented by triples of the form (x,y,r). Mutation operators can bump the coordinates in various ways (e.g. a normally distributed increment). Cross over would be something like e.g. (a,b,c) x (d,e,f) => (a,e,f), (d,b,c). You need an objective function. Conceptually it is area -- but it is hard to make sure that the constraints are always satisfied. What you could do is use as an objective function the area minus a penalty for each violated constraint. The penalty can be adjusted to eventually kill-off all circles which violate the constraint but shouldn't be so large that it prevents all parts of the solution space from being explored. Such parameters often need to be tweaked on a trial-and-error basis.

What is the shortest path that touches every tile in a square grid? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Suppose a ant is placed on the position (0,0) of a chess-board. That ant wants to walk through every single tile of the board, while walking the least it can to do so. What path it must follow? Is there a formula F(i) that returns the position of the ith tile on that path?
Edit: as requested, I've tried the following:
I tried googling for keywords such as "shortest path", "shortest path in square grid", but couldn't find anything relevant.
I then downloaded, configured and used a Traveling Salesman Problem solver in a square grid. Obviously, the solution wasn't satisfactory, but I could gain an insight on the problem. There is an illustration of my results:
I then, intuitively, speculated wether the answer could be something like the Hilbert Curve: . I googled about it and asked on a IRC programming channel, but I couldn't find any actual evidence this is better than spirals and similars, nor a proof this is the best possible solution.
EDIT 2: Further clarifications:
The ant can move diagonally. The distance refers to the euclidean length of the line defined by the path.
Walk in straight line, with the edge of the board on your left, until you either hit the edge of the chess board or a tile you have visited before. If you do, then take a right.
Or a thousand other obvious patterns.
Any path that takes 63 steps is the minimum and just as good as any other path.
This is going to depend on if you're taking the width of each square into consideration or is this just a double array question?
If we're talking a double-array question f(x,y), then the answer is that there is no least path because the ant will need to travel to each square f(x,y) = x*y, so f(8,8) = 64.
If we start taking the width of the tiles themselves into consideration, then the answer is somewhat different because we can use some strategies to get the least amount of distance traveled (such as starting in the center, staying by the grid separators and walking in a roughly spiral pattern r=xy^(theta)).

how to fit polygon inside another polygon [closed]

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 8 years ago.
Improve this question
I have two polygons as shown in the image below.
The left one is "rough polygon" and the right one is "final polygon"
Now, I'm looking for algorithm to fit "final polygon" inside "rough polygon" with best maximum scale.
you can rotate as well as translate "final polygon" as much as you want.
you can't perform individual x dimension or y dimension scaling.
you can only perform uniform scaling (where value of Sx and Sy are same).
Here is a possible line of attack for an exact solution by exhaustive trials; just ideas.
My guess is that a solution is achieved when there are three contacts. I mean three vertexes of either polygon touching an edge of the other or conversely. (If there are less than three contacts, you can inflate the internal polygon so that it comes into a third contact.)
Given two arbitrary triangles, it shouldn't be so difficult to find all possible three-contact positions.
So the global scheme is to take all triples of vertexes/sides from one polygon, and take all complementary triples of sides/vertexes of the other. For every combination, momentarily consider that you have triangles and find the possible three-contact positions. For for every candidate position check if the inner polygon stays confined in the outer one. In the end, keep the admissible solution with the largest scale factor.
For polygons with N and M sides, there will be O(N³M³) configurations to try, and the containment test can be as costly as O(NM). So this approach is only viable for very small polygons.
Scale the rightside polygon by 0.01. (geometrical)
Start spinnning it so fast that it draws circle. (geometrical)
Start incrementing the scale 0.01 by 0.01. (geometrical)
Stop when it touches the outer polygon. (geometrical)
Then bounce it to opposite direction until it bounces again. (physical)
Again and again.(iterations)
Until it cannot move/bounce again.(stuck optimally) (physical)
Use simulated annealing in case of false local solutions.(you need global solution)

Given a point inside a rectangle, determine the side that's closest to the point [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I was curious if there was an elegant way to do this, aside from just calculating the distance from the point to each side and finding the minimum.
Some things I've thought about:
If it's a square, we can just draw the diagonals and figure out which of the 4 regions the point falls on. Each of these region corresponds to a closest side.
Perhaps we can divide up the rectangle into squares and go somewhere from there?
It seems an alternative solution would be too complicated and not worth looking for.
For rectangle you can use following regions:
I think the rectangle is not orthogonal to the coordinate system. First calculate the middle point of every side. This should be simple depending on how you have define the rectangle.
Then calculate the distance to this middle points. The smallest distance is the nearest side. You need not to calculate the full distance with pytagoras. The sum of the squared is enough.

Resources