9front - general discussion about 9front
 help / color / mirror / Atom feed
* rotate: perform arbitrary rotations
@ 2020-02-16 18:43 rgl
  2020-02-16 19:34 ` [9front] " Eli Cohen
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: rgl @ 2020-02-16 18:43 UTC (permalink / raw)
  To: 9front

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

hi all,

would it be OK to commit this?


-rodri

[-- Attachment #2: Type: text/plain, Size: 1463 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 19:37:26 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)
 {
@@ -92,10 +135,11 @@
 main(int argc, char *argv[])
 {
 	Memimage *m;
-	int fd, r;
+	int fd, r, wasright;
 	char f;
 
 	f = 0;
+	wasright = 0;
 	r = 0;
 	fd = 0;
 	ARGBEGIN {
@@ -131,8 +175,11 @@
 		m = rot90(m);
 	case 90:
 		m = rot90(m);
+		wasright++;
 		break;
 	}
+	if(!wasright)
+		m = rotate(m, -r*PI/180);
 	if(writememimage(1, m) < 0)
 		sysfatal("writememimage: %r");
 	exits(0);

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-02-18 14:43 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-16 18:43 rotate: perform arbitrary rotations rgl
2020-02-16 19:34 ` [9front] " Eli Cohen
2020-02-16 20:01   ` rgl
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

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).