From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from relay1-d.mail.gandi.net ([217.70.183.193]) by ewsd; Sun Feb 16 15:01:13 EST 2020 X-Originating-IP: 185.198.110.254 Received: from coeus.antares-labs.eu (unknown [185.198.110.254]) (Authenticated sender: rgl@antares-labs.eu) by relay1-d.mail.gandi.net (Postfix) with ESMTPSA id 47846240008 for <9front@9front.org>; Sun, 16 Feb 2020 20:01:05 +0000 (UTC) Message-ID: <6A5BF5F0DFD500FCB81E30369DC198CB@antares-labs.eu> Date: Sun, 16 Feb 2020 21:01:02 +0100 From: rgl@antares-labs.eu To: 9front@9front.org Subject: Re: [9front] rotate: perform arbitrary rotations In-Reply-To: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="upas-zvwtwksnqzfbobrsmkcyldveuz" List-ID: <9front.9front.org> List-Help: X-Glyph: ➈ X-Bullshit: compliant engine software This is a multi-part message in MIME format. --upas-zvwtwksnqzfbobrsmkcyldveuz Content-Disposition: inline Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit this one is better. i'll try to come up with a crop-free method. --upas-zvwtwksnqzfbobrsmkcyldveuz Content-Disposition: inline Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit diff -r 287d82efe5f2 sys/src/cmd/rotate.c --- a/sys/src/cmd/rotate.c Mon Jan 13 23:22:35 2020 +0000 +++ b/sys/src/cmd/rotate.c Sun Feb 16 20:55:33 2020 +0100 @@ -3,6 +3,49 @@ #include #include +double +round(double n) +{ + return floor(n + 0.5); +} + +Point +rotatept(Point p, double θ, Point c) +{ + Point r; + + p = subpt(p, c); + r.x = round(p.x*cos(θ) - p.y*sin(θ)); + r.y = round(p.x*sin(θ) + p.y*cos(θ)); + r = addpt(r, c); + return r; +} + +Memimage* +rotate(Memimage *m, double θ) +{ + Memimage *w; + Rectangle r; + Point p, c; + + if((w = allocmemimage(m->r, m->chan)) == nil) + sysfatal("allocmemimage: %r"); + memfillcolor(w, DTransparent); + c = divpt(addpt(m->r.min, m->r.max), 2); + for(r = m->r; r.min.y < m->r.max.y; r.min.y++){ + r.min.x = m->r.min.x; + r.max.y = r.min.y+1; + for(; r.min.x < m->r.max.x; r.min.x++){ + r.max.x = r.min.x+1; + p = rotatept(r.min, θ, c); + if(ptinrect(p, m->r)) + memimagedraw(w, r, m, p, nil, ZP, S); + } + } + freememimage(m); + return w; +} + Memimage* rot90(Memimage *m) { @@ -124,15 +167,10 @@ if(f == 'l') r = 180; } - switch(r % 360){ - case 270: + for(r %= 360; r >= 90; r -= 90) m = rot90(m); - case 180: - m = rot90(m); - case 90: - m = rot90(m); - break; - } + if(r > 0) + m = rotate(m, -r*PI/180); if(writememimage(1, m) < 0) sysfatal("writememimage: %r"); exits(0); --upas-zvwtwksnqzfbobrsmkcyldveuz--