Блог пользователя I_love_tigersugar

Автор I_love_tigersugar, 11 лет назад, По-английски

Given a non self-intersect polygon and a line. How can I calculate the total length of parts of line which are inside the polygon? I need an O(NlogN) or faster algorithm, where N is the number of vertices of polygon.

For example, below is the polygon ABCDEFGH.

With line y = 0, no parts inside the polygon.

With line y = 2, the parts inside the polygon are IJ and KL, and the total length is 2.5.

With line y = 3, the part inside the polygon is BC, which has length 1,

Image and video hosting by TinyPic

I'm looking for your answers. Thanks for your help.

  • Проголосовать: нравится
  • +13
  • Проголосовать: не нравится

»
11 лет назад, скрыть # |
 
Проголосовать: нравится -13 Проголосовать: не нравится

I'm also !

»
11 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +28 Проголосовать: не нравится

Do you know how to cut a polygon with a line? It is similar. The main idea is to keep track of where you are respectively to the line while going through vertices of the polygon.

If you do this properly, you get 2k points of intersection between the line and the polygon. Then you can easily get the total length.

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I think that we can sort the intersections of the given line with the polygon's edges by their X-coordinate (tie by Y-coordinate). Then the result should be the total distance between every pair of points i and (i+1) (with i%2=1)

»
11 лет назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

Let's divide the polygon trapeze height 1 (h = 1)

For trapeze as AQH (point Q(0;1) ) or GHF we consider that the base is 0 (points A or G)

Let's count areas of all trapeze (S (trapeze) = h * (a + b) / 2, h = 1 => S (trapeze) = (a + b) / 2)

S(AQH) = QH / 2, S(GHF) = FH / 2 = (HD + DF) / 2 S(QIJD) = (IJ + QD) / 2 = (IJ + QH + HD) / 2 S(DKLF) = (KL + DF) / 2 S(IBCJ) = (BC + IJ) / 2, S(KEL) = KL / 2

if we sum all areas, we'll have: 1)area of polygon 2)QH + HD + DF + IJ + KL + BC/2 BC — horizontal side

So we should count half-sum of all horizontal sides of the polygon, because we consider them once

So ans = Area of polygon + Half-sum of all horizontal sides of the polygon

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I'm looking for a site where I can submit my solution. Would you help providing the link?

»
11 лет назад, скрыть # |
Rev. 5  
Проголосовать: нравится +2 Проголосовать: не нравится

Edit: sorry, misread the statement.