From ca3289c29673b914d8ca64b08954ef0afccdf3c9 Mon Sep 17 00:00:00 2001 From: rodri Date: Mon, 9 Oct 2023 21:01:52 +0000 Subject: replace the andy layout algorithm for a better one. --- andy.c | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) (limited to 'andy.c') diff --git a/andy.c b/andy.c index 8624822..a1de23d 100644 --- a/andy.c +++ b/andy.c @@ -67,34 +67,21 @@ turnaround(Andy *a) a->disengage(a); } -static int -between(double n, double min, double max) -{ - return n >= min && n < max; -} - static int lineXline(Point2 min0, Point2 max0, Point2 min1, Point2 max1) { - double a₁, b₁; - double a₂, b₂; - double det; - - a₁ = max0.y - min0.y; - b₁ = min0.x - max0.x; - - a₂ = max1.y - min1.y; - b₂ = min1.x - max1.x; - - det = a₁*b₂ - a₂*b₁; - if(det == 0){ - /* do they overlap? */ - if((min0.x == min1.x && (between(min0.y, min1.y, max1.y) || between(max0.y, min1.y, max1.y))) || - (min0.y == min1.y && (between(min0.x, min1.x, max1.x) || between(max0.x, min1.x, max1.x)))) - return 1; - return 0; - } - return 1; + if(min0.x == max0.x) + max0.x++; + else if(min0.y == max0.y) + max0.y++; + + if(min1.x == max1.x) + max1.x++; + else if(min1.y == max1.y) + max1.y++; + + return min0.x < max1.x && min1.x < max0.x && + min0.y < max1.y && min1.y < max0.y; } static void @@ -107,7 +94,7 @@ andy_layout(Andy *a, Msg *m) for(i = 0; i < NSHIPS; i++){ Retry: cells[i] = Pt2(getrand(MAPW-shiplen(i)), getrand(MAPH-shiplen(i)), 1); - o[i] = getrand(1)? OH: OV; + o[i] = i > 1 && o[i-1] != OH? OH: OV; sv[i] = o[i] == OH? Vec2(1,0): Vec2(0,1); for(j = 0; j < i; j++) if(lineXline(cells[i], addpt2(cells[i], mulpt2(sv[i], shiplen(i))), -- cgit v1.2.3