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

* Re: [9front] rotate: perform arbitrary rotations
  2020-02-16 18:43 rotate: perform arbitrary rotations rgl
@ 2020-02-16 19:34 ` Eli Cohen
  2020-02-16 20:01   ` rgl
  2020-02-16 19:59 ` Steve Simon
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: Eli Cohen @ 2020-02-16 19:34 UTC (permalink / raw)
  To: 9front

nice! all in favor say meow

On Sun, Feb 16, 2020 at 10:44 AM <rgl@antares-labs.eu> wrote:
>
> hi all,
>
> would it be OK to commit this?
>
>
> -rodri
> 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);



-- 
http://echoline.org


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

* Re: [9front] rotate: perform arbitrary rotations
  2020-02-16 18:43 rotate: perform arbitrary rotations rgl
  2020-02-16 19:34 ` [9front] " Eli Cohen
@ 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
  3 siblings, 1 reply; 7+ messages in thread
From: Steve Simon @ 2020-02-16 19:59 UTC (permalink / raw)
  To: 9front


rotating every point is correct but slow,
perhaps it would be worth putting the effort in to implement the triple shear algorithm?

-Steve


> On 16 Feb 2020, at 6:44 pm, rgl@antares-labs.eu wrote:
> 
> ARGBEGIN {



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

* Re: [9front] rotate: perform arbitrary rotations
  2020-02-16 19:34 ` [9front] " Eli Cohen
@ 2020-02-16 20:01   ` rgl
  0 siblings, 0 replies; 7+ messages in thread
From: rgl @ 2020-02-16 20:01 UTC (permalink / raw)
  To: 9front

[-- 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);

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

* Re: [9front] rotate: perform arbitrary rotations
  2020-02-16 19:59 ` Steve Simon
@ 2020-02-16 20:05   ` rgl
  0 siblings, 0 replies; 7+ messages in thread
From: rgl @ 2020-02-16 20:05 UTC (permalink / raw)
  To: 9front

thanks steve, i will do some research about it this week.



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

* Re: [9front] rotate: perform arbitrary rotations
  2020-02-16 18:43 rotate: perform arbitrary rotations rgl
  2020-02-16 19:34 ` [9front] " Eli Cohen
  2020-02-16 19:59 ` Steve Simon
@ 2020-02-17 23:33 ` cinap_lenrek
  2020-02-18 14:43 ` ori
  3 siblings, 0 replies; 7+ messages in thread
From: cinap_lenrek @ 2020-02-17 23:33 UTC (permalink / raw)
  To: 9front

IST EIN *NEIN* STACKENBLOCHEN!!!

--
cinap


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

* Re: [9front] rotate: perform arbitrary rotations
  2020-02-16 18:43 rotate: perform arbitrary rotations rgl
                   ` (2 preceding siblings ...)
  2020-02-17 23:33 ` cinap_lenrek
@ 2020-02-18 14:43 ` ori
  3 siblings, 0 replies; 7+ messages in thread
From: ori @ 2020-02-18 14:43 UTC (permalink / raw)
  To: rgl, 9front

> hi all,
> 
> would it be OK to commit this?
> 
> 
> -rodri
> 
> +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;
> +}

This is probably not going to give very good results,
since you're not doing any filtering.

There are good algorithms for doing affine transformations.
It's probably worth doing that.



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