9front - general discussion about 9front
 help / color / mirror / Atom feed
From: rgl@antares-labs.eu
To: 9front@9front.org
Subject: Re: [9front] rotate: perform arbitrary rotations
Date: Sun, 16 Feb 2020 21:01:02 +0100	[thread overview]
Message-ID: <6A5BF5F0DFD500FCB81E30369DC198CB@antares-labs.eu> (raw)
In-Reply-To: <CAHwi9bybMEGO8B4Z=jo=HH84aUtBnTmOFY5DF6XCBbodsdbeFQ@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 66 bytes --]

this one is better.

i'll try to come up with a crop-free method.

[-- Attachment #2: Type: text/plain, Size: 1398 bytes --]

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 <draw.h>
 #include <memdraw.h>
 
+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);

  reply	other threads:[~2020-02-16 20:01 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-16 18:43 rgl
2020-02-16 19:34 ` [9front] " Eli Cohen
2020-02-16 20:01   ` rgl [this message]
2020-02-16 19:59 ` Steve Simon
2020-02-16 20:05   ` rgl
2020-02-17 23:33 ` cinap_lenrek
2020-02-18 14:43 ` ori

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6A5BF5F0DFD500FCB81E30369DC198CB@antares-labs.eu \
    --to=rgl@antares-labs.eu \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).