hedgewars/uLandGenTemplateBased.pas
changeset 10205 fc99e124ba4d
parent 10204 50e52e511300
child 10206 979a663d7351
equal deleted inserted replaced
10204:50e52e511300 10205:fc99e124ba4d
    94     BezierizeEdge(pa, _0_1);
    94     BezierizeEdge(pa, _0_1);
    95 end;
    95 end;
    96 
    96 
    97 procedure FindPoint(si: LongInt; var newPoint: TPoint; var pa: TPixAr);
    97 procedure FindPoint(si: LongInt; var newPoint: TPoint; var pa: TPixAr);
    98 const mapBorderMargin = 30;
    98 const mapBorderMargin = 30;
    99     minDistance = 20;
    99     minDistance = 32;
   100 var p1, p2, mp: TPoint;
   100 var p1, p2, p4, fp, mp: TPoint;
   101     i, t1, t2, a, b, p, q, iy, ix, aqpb: LongInt;
   101     i, t1, t2, a, b, p, q, iy, ix, aqpb: LongInt;
   102     dab, d, distL, distR: LongInt;
   102     dab, d, distL, distR: LongInt;
   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];
   148         d:= DistanceI(mp.y - LAND_HEIGHT + mapBorderMargin, mp.x - ix).Round;
   148         d:= DistanceI(mp.y - LAND_HEIGHT + mapBorderMargin, mp.x - ix).Round;
   149         if t2 > 0 then distR:= min(d, distR) else distL:= min(d, distL);
   149         if t2 > 0 then distR:= min(d, distR) else distL:= min(d, distL);
   150     end;
   150     end;
   151 
   151 
   152     // now go through all other segments
   152     // now go through all other segments
       
   153     fp:= pa.ar[0];
   153     for i:= 0 to pa.Count - 2 do
   154     for i:= 0 to pa.Count - 2 do
   154         if (i <> si) and (pa.ar[i].x <> NTPX) and (pa.ar[i + 1].x <> NTPX) then
   155         if pa.ar[i].x = NTPX then
   155         begin
   156             fp:= pa.ar[i + 1]
       
   157         else if (i <> si) then
       
   158         begin
       
   159         p4:= pa.ar[i + 1];
       
   160         if p4.x = NTPX then
       
   161             p4:= fp;
       
   162         
   156             // check if it intersects
   163             // check if it intersects
   157             t1:= (mp.x - pa.ar[i].x) * b - a * (mp.y - pa.ar[i].y);
   164             t1:= (mp.x - pa.ar[i].x) * b - a * (mp.y - pa.ar[i].y);
   158             t2:= (mp.x - pa.ar[i + 1].x) * b - a * (mp.y - pa.ar[i + 1].y);
   165             t2:= (mp.x - p4.x) * b - a * (mp.y - p4.y);
   159 
   166 
   160             if (t1 > 0) <> (t2 > 0) then // yes it does, hard arith follows
   167             if (t1 > 0) <> (t2 > 0) then // yes it does, hard arith follows
   161             begin
   168             begin
   162                 p:= pa.ar[i + 1].x - pa.ar[i].x;
   169                 p:= p4.x - pa.ar[i].x;
   163                 q:= pa.ar[i + 1].y - pa.ar[i].y;
   170                 q:= p4.y - pa.ar[i].y;
   164                 aqpb:= a * q - p * b;
   171                 aqpb:= a * q - p * b;
   165 
   172 
   166                 if (aqpb <> 0) then
   173                 if (aqpb <> 0) then
   167                 begin
   174                 begin
   168                     // (ix; iy) is intersection point
   175                     // (ix; iy) is intersection point
   176                     t1:= b * (mp.y - iy) + a * (mp.x - ix);
   183                     t1:= b * (mp.y - iy) + a * (mp.x - ix);
   177                     if t1 > 0 then distL:= min(d, distL) else distR:= min(d, distR);
   184                     if t1 > 0 then distL:= min(d, distL) else distR:= min(d, distR);
   178                 end;
   185                 end;
   179             end;
   186             end;
   180         end;
   187         end;
   181 
       
   182     // go through all points
   188     // go through all points
   183     for i:= 0 to pa.Count - 2 do
   189     for i:= 0 to pa.Count - 2 do
   184         // if this point isn't on current segment
   190         // if this point isn't on current segment
   185         if (si <> i) and (i <> si + 1) then
   191         if (si <> i) and (i <> si + 1) and (pa.ar[i].x <> NTPX) then
   186         begin
   192         begin
   187             // also check intersection with rays through pa.ar[i] if this point is good
   193             // also check intersection with rays through pa.ar[i] if this point is good
   188             t1:= (p1.x - pa.ar[i].x) * b - a * (p1.y - pa.ar[i].y);
   194             t1:= (p1.x - pa.ar[i].x) * b - a * (p1.y - pa.ar[i].y);
   189             t2:= (p2.x - pa.ar[i].x) * b - a * (p2.y - pa.ar[i].y);
   195             t2:= (p2.x - pa.ar[i].x) * b - a * (p2.y - pa.ar[i].y);
   190             if (t1 > 0) <> (t2 > 0) then
   196             if (t1 > 0) <> (t2 > 0) then