hedgewars/uLandGenTemplateBased.pas
changeset 10206 979a663d7351
parent 10205 fc99e124ba4d
child 10207 9dd3a44805a1
equal deleted inserted replaced
10205:fc99e124ba4d 10206:979a663d7351
   103 begin
   103 begin
   104     // [p1, p2] is segment we're trying to divide
   104     // [p1, p2] is segment we're trying to divide
   105     p1:= pa.ar[si];
   105     p1:= pa.ar[si];
   106     p2:= pa.ar[si + 1];
   106     p2:= pa.ar[si + 1];
   107 
   107 
       
   108     if p2.x = NTPX then
       
   109     // it is segment from last to first point, so need to find first point
       
   110     begin
       
   111         i:= si - 2;
       
   112         while (i >= 0) and (pa.ar[i].x <> NTPX) do
       
   113             dec(i);
       
   114         p2:= pa.ar[i + 1]
       
   115     end;
       
   116 
   108     // perpendicular vector
   117     // perpendicular vector
   109     a:= p2.y - p1.y;
   118     a:= p2.y - p1.y;
   110     b:= p1.x - p2.x;
   119     b:= p1.x - p2.x;
   111     dab:= DistanceI(a, b).Round;
   120     dab:= DistanceI(a, b).Round;
   112 
   121 
   113     if (p1.x = NTPX) or (p2.x = NTPX) or (dab < minDistance * 3) then
       
   114     begin
       
   115         newPoint:= p1;
       
   116         exit;
       
   117     end;
       
   118 
       
   119     // its middle point
   122     // its middle point
   120     mp.x:= (p1.x + p2.x) div 2;
   123     mp.x:= (p1.x + p2.x) div 2;
   121     mp.y:= (p1.y + p2.y) div 2;
   124     mp.y:= (p1.y + p2.y) div 2;
       
   125 
       
   126     // don't process too short segments or those which are too close to map borders
       
   127     if (p1.x = NTPX)
       
   128             or (dab < minDistance * 3) 
       
   129             or (mp.x < mapBorderMargin)
       
   130             or (mp.x > LAND_WIDTH - mapBorderMargin)
       
   131             or (mp.y < mapBorderMargin)
       
   132             or (mp.y > LAND_HEIGHT - mapBorderMargin)
       
   133     then
       
   134     begin
       
   135         newPoint:= p1;
       
   136         exit;
       
   137     end;
   122 
   138 
   123     // find distances to map borders
   139     // find distances to map borders
   124     if a <> 0 then
   140     if a <> 0 then
   125     begin
   141     begin
   126         // left border
   142         // left border
   157         else if (i <> si) then
   173         else if (i <> si) then
   158         begin
   174         begin
   159         p4:= pa.ar[i + 1];
   175         p4:= pa.ar[i + 1];
   160         if p4.x = NTPX then
   176         if p4.x = NTPX then
   161             p4:= fp;
   177             p4:= fp;
   162         
   178 
   163             // check if it intersects
   179             // check if it intersects
   164             t1:= (mp.x - pa.ar[i].x) * b - a * (mp.y - pa.ar[i].y);
   180             t1:= (mp.x - pa.ar[i].x) * b - a * (mp.y - pa.ar[i].y);
   165             t2:= (mp.x - p4.x) * b - a * (mp.y - p4.y);
   181             t2:= (mp.x - p4.x) * b - a * (mp.y - p4.y);
   166 
   182 
   167             if (t1 > 0) <> (t2 > 0) then // yes it does, hard arith follows
   183             if (t1 > 0) <> (t2 > 0) then // yes it does, hard arith follows