Re: Coordinate from device to user space without a context

Donn <[email protected]>
Newsgroups gmane.comp.lib.cairo
Message-ID <[email protected]>
On 07/11/2013 20:22, Bryce W. Harrington wrote:
> If I were you I'd probably start afresh with just
> the formulas I gave you, and make a little toy app that *just* does the
> mouse -> userspace conversion, without any other matrix calls.

Bryce, I spend some time making a toy app as you suggested. It's giving 
confusing results, but I hope it will help the list help me! Attached.

System info:
valac 0.20.1
cairo114.vapi

On my system I compile it with:
valac --pkg cairo -X -w mailing_list_matrix_testbed.vala -o test
And run:
./test yes #will do the cersion with the formulas
./test #will use device_to_user
then view the output png:
feh img.png

It's confusing because *both* versions do the same wrong thing! I was 
not expecting that. The odd oscillation in the numbers is at work.


> Btw, when you make cr.translate(), cr.rotate(), and cr.scale() calls,
> those are really just helper routines for modifying the internal
> matrix.  So be aware of those too.
Thanks, I will dispense with these calls in favour of a direct 
set_matrix in the future, when I can tell the trees from the woods.


Thanks for all the patience so far, I worry that I'm not comprehending 
fast enough!
\d

-- 
cairo mailing list
[email protected]
http://lists.cairographics.org/mailman/listinfo/cairo
mailing_list_matrix_testbed.vala (text/x-vala, 2.4 KB)
using Cairo;
/*
valac --pkg cairo -X -w mailing_list_matrix_testbed.vala -o test
And run:
./test yes #will do the version with the formulas
./test #will use device_to_user
then view the output png:
feh img.png
*/
void main(string[] args) {

  bool flag = false;
  if (args.length > 0) {
    flag = (args[1] == "yes");
  }
  if (flag) stdout.printf("RUNNING WITH FORMULA\n");
  if (!flag) stdout.printf("RUNNING WITH CONTEXT METHOD\n");

  Cairo.Matrix mymatrix;

	Cairo.ImageSurface surface = new Cairo.ImageSurface (Cairo.Format.ARGB32, 500, 500);
	Cairo.Context cr = new Cairo.Context (surface);

  //centre the axes - 0,0 in middle with -x left and +x right. Range(-250,250)
  var matrix = Cairo.Matrix (1, 0, 0, 1, 250,250 );
  matrix.scale (1, 1); 
	cr.transform(matrix);
  cr.save();

  double user_x=0; double user_y=0;
  double dev_y=0;
  double tmp_d_x; double tmp_d_y;

  cr.save(); 
    cr.translate(-100,0); //This is my "parent" scope. [A]

    cr.arc (0.0, 0.0, 80.0, 0, 2*Math.PI);
    cr.set_source_rgb(1,0,0);
    cr.fill();

    //Draw many children in scope [A] - this loop is device coords, like a mouse.
    for (double dev_x=0; dev_x<250; dev_x+=10) {
    cr.save(); //Preserve [A]

        //Position relative to parent scope [A]
        cr.translate(user_x,user_y); cr.scale(1,1); //this is the child scope.[B]

        mymatrix = cr.get_matrix(); //For formula version of run.
          //Draw a green dot.
          cr.arc (0.0, 0.0, 30.0, 0, 2*Math.PI);
          cr.set_source_rgb(0,1,0);
          cr.fill();

        //Formula run - changing device coords mapped into scope [B].
        if (flag) {
          //stdout.printf("mymatrix:  %g|%g|%g|%g|%g|%g\n",mymatrix.xx ,mymatrix.yx ,mymatrix.xy ,mymatrix.yy ,mymatrix.x0 ,mymatrix.y0 );
          user_x = (dev_x - mymatrix.x0)/mymatrix.xx;
          user_y = (dev_y - mymatrix.y0)/mymatrix.yy;
        } else {
          //Device_to_user run - changing device coords into scope [B]
          tmp_d_x = dev_x; tmp_d_y = dev_y;
          cr.device_to_user(ref tmp_d_x, ref tmp_d_y);
          user_x = tmp_d_x; user_y = tmp_d_y;
        }

        stdout.printf("dev_x dev_y: %G|%G becomes user_x user_y: %G|%G\n",dev_x,dev_y, user_x, user_y);

    cr.restore(); //End scope [B], restore to [A]
    } //draw next child

  cr.restore(); //Ends scope [A]

  cr.restore(); //Ends axes centering
	surface.write_to_png ("img.png");
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.