3

I am trying to write an algorithm to find the upper envelop (skyline) of triangles. By following you can see the skyline of rectangles: enter image description here

I have an algorithm for merging two skylines(L and R) of rectangles as follows:

  • represent each rectangle by (x1, x2, h) where h is height and x2-x1 is width
  • represent each skyline by a list of couple (x, h)
  • for i= min(L[ 1].x, R[ 1].x) to max(L[size of L].x, R[size of R].x) choose max(L[i].h, R[i].h)

Now, my question is how can I represent triangle and how I can merge skylines of two triangles

any idea will be appreciated

3
  • 5
    This sounds like a general programming problem. It's (currently) tagged as C++ but I don't see anything language-specific in it. Not even mention of C++. Commented Dec 4, 2011 at 12:25
  • Regarding representation I think it would start from simply the lines. Commented Dec 4, 2011 at 12:35
  • Is there any chance you'd be willing to elaborate a bit further on your skyline for RECtangles? Commented Feb 20, 2013 at 9:43

1 Answer 1

3

In the following I assume that the triangles are with their baseline on the bottom. I'm also assuming that all triangles are such that the upper corner is above the baseline (i.e. if you go straight down from the upper corner, you get inside the triangle, not outside). However I'm not assuming that only symmetric triangles are allowed.

Actually a merging of triangles will give a skyline where simply points are connected with lines. so the representation of a triangle skyline could just be a ordered list of points (x_i, y_i) with the restriction that y_0 = 0 and y_N = 0 where N is the index of the last point. A single triangle would then be represented by the three-element list (x_0, 0), (x_1, h), (x2,0) where x_0 and x_2 are the left and right endpoint (the two points where the triangle reaches 0), x_1 gives the horizontal position of the upper corner, and h gives the height.

The merging of two skylines can then for example be done as follows:

Step 1: For each line segment (x_i, y_i)--(x_{i+1}, y_{i+1}) from skyline 1 and each line segment (x_j,y_j)--(x_{j+1},y_{j+1}) calculate whether they intersect, and if so, where (this means solving a simple system of two linear equations). Collect the intersection points into a new list, intersections. So now you have three lists of points: skyline1, skyline2 and intersections. Since all intersections will be part of the skyline, use that as the basis for the new Skyline. (a special case is when both skylines agree over an interval, but in such intervals the combined skyline is the same as each single one anyway, so just use the start and end points of those intervals as intersection points)

Now for each pair of intersection points (and also left of the first intersection and right of the last intersection), there will be always exactly one skyline which is above the other (unless they agree, but then it doesn't matter which you choose). Add the points in the interval from that skyline to your combined skyline. You find out the larger one by just choosing an arbitrary point of one skyline (except if the intersection point is also a skyline point, that one should not be chosen) and detect the height of the other skyline at its x value (if the other skyline also has a point at the same x value, it's a simple comparison of the y value, otherwise you have to interpolate the y values of the preceding and following points).

After doing that, you should have the correct combined skyline.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.