gnome-canvas-coordinates.html

来自「linux下gnome编程」· HTML 代码 · 共 426 行

HTML
426
字号
<HTML><HEAD><TITLE>Coordinate Systems</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.61"><LINKREL="HOME"TITLE="Writing GNOME Applications"HREF="index.html"><LINKREL="UP"TITLE="The GNOME Canvas"HREF="gnome-canvas.html"><LINKREL="PREVIOUS"TITLE="The GNOME Canvas"HREF="gnome-canvas.html"><LINKREL="NEXT"TITLE="Using the Canvas"HREF="gnome-canvas-using.html"></HEAD><BODYCLASS="SECT1"><DIVCLASS="NAVHEADER"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Writing GNOME Applications</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="gnome-canvas.html">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 11. The GNOME Canvas</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="gnome-canvas-using.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="GNOME-CANVAS-COORDINATES">Coordinate Systems</A></H1><P>        As a side effect of its complex needs, the GNOME Canvas must        use more than one coordinate system to express the locations        of its objects at different times.  Each coordinate system has        a slightly different meaning, as described in Sections 11.2.1        through 11.2.4. The Canvas provides several helper functions        to make it easy for you to transform from one system to        another. Internally, the Canvas uses affine transforms (see        Section 10.4.5) to perform these translations, but you don't        need to know this to use the functions. You can, however,        directly access these affine matrices if you need to.      </P><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN1049">World Coordinates</A></H2><P>          The Canvas uses a logical, abstracted coordinate space as          its native, internal coordinate system. This coordinate          system, called world coordinates, is used not for rendering          operations (the Canvas must transform to a pixel-oriented          coordinate space for that), but rather for exact,          conceptual placement of objects.  World coordinates are          expressed as unbounded floating-point numbers, so 10.6221          and -335.0105 are equally valid, although depending on the          visible area, they may or may not appear on the rendered          Canvas.        </P><P>          Like most GUI-related coordinates, world coordinates have an          inverted y-axis; that is, the positive numbers become larger          as you move down your screen. Unlike pixel-based screen          coordinates, the negative values of which are always          off-screen, the negative world coordinates can be visible          just like positive coordinates. It all depends on how you          set up the viewing area. Thus the world coordinates origin          at (0.0, 0.0) does not necessarily have to correspond to          the upper left corner of the Canvas widget. It can be offset          by an arbitrary amount.        </P><P>          The Canvas will convert world coordinates into screen          coordinates when it's ready to render itself to the drawing          buffer, so you can use whatever scale is most convenient for          you. World coordinates need not have a one-to-one cor-          relation to screen pixels. One thing you should keep in mind          is that the aspect ratio between the x and y coordinates          does not change between world and screen coordinate          systems. You cannot stretch your coordinate system in one          direction without stretching it equally in the other          direction.        </P><P>          You can choose the range of world coordinates when you set          up the Canvas, so you should settle on something that makes          sense in your application. For example, don't use negative          numbers unless something in your display model specifically          calls for it. In most cases, positive-only world co-          ordinates work fine.        </P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN1055">Item Coordinates</A></H2><P>          Although world coordinates represent the native, global          space of the Canvas, each individual Canvas item has its own          private coordinate space, expressed in the same units as the          world coordinate system. In fact, item coordinates are more          or less world coordinates offset so that the origin falls at          the upper left corner of the item in question. Thus a point          at world coordinates (125.0, 150.0) on a Canvas item located          at (100.0, 100.0) would have item coordinates of (25.0,          50.0) for that particular item. The same point might have          different item coordinates for a second Canvas item if that          Canvas item were offset from the first Canvas item.        </P><P>          The Canvas comes with a handful of easy-to-use functions to          convert between coordinate systems. The following          functions convert between world coordinates and item          coordinates. All Canvas items carry a pointer to the Canvas          that owns them, so it's not necessary to supply the Canvas          as a separate parameter:        </P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gnome_canvas_item_w2i(GnomeCanvasItem *item,     double *x, double *y);void gnome_canvas_item_i2w(GnomeCanvasItem *item,    double *x, double *y);        </PRE></TD></TR></TABLE><P>          To use these functions, pass in pointers to two gdouble          variables for the x and y coordinates. You should set the x          and y parameters to the values you want to convert; the          function will return the remapped coordinates in the same          variables. In terms of our earlier example, the code might          look like this:        </P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">/* Find item coordinates at world coordinates (125.0, 150.0) *//* Item is located at (100.0, 100.0) */gdouble xcoord = 125.0;gdouble ycoord = 150.0;/* Convert to item coordinates */gnome_canvas_item_w2i(item, &#38;xcoord, &#38;ycoord);/* xcoord == 25.0, ycoord == 50.0 *//* Convert back to world coordinates (round-trip) */gnome_canvas_item_i2w(item, &#38;xcoord, &#38;ycoord);/* xcoord == 125.0, ycoord == 150.0 */        </PRE></TD></TR></TABLE><P>          Occasionally you'll want to access the raw affine transforms          that the Canvas uses to perform the above translations. The          following functions will do the trick:        </P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gnome_canvas_item_i2w_affine(GnomeCanvasItem *item,     double affine[6]);void gnome_canvas_item_w2i_affine(GnomeCanvasItem *item,     double affine[6]);        </PRE></TD></TR></TABLE></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN1064">Canvas Coordinates</A></H2><P>          Eventually the Canvas must translate its abstract internal          coordinate system into concrete integer-based coordinates          for rendering to the RGB buffer, and eventually onto the          screen. Because pixel buffers tend to be zero-based arrays,          they are usually expressed with positive-only integer          coordinates, ranging from the origin in the upper left          corner, downward and to the right. In the Canvas these          coordinates are referred to as canvas coordinates.        </P><P>          Again, the Canvas uses affine transformations to convert          from one system to the other. This makes it possible to          handle offsets and Canvas scaling. When the time comes to          render, the Canvas can quite easily convert a world coordi-          nate area with a bounding box defined by (-500.0, -500.0,          +500.0, +500.0) into pixel-based canvas coordinates of (0,          0, 150, 150), for example. The Canvas will take care of          these transformations internally. As we'll see in Section          11.3.2, all you have to do is set the viewable area and the          scaling factor, and the Canvas will handle the rest.        </P><P>          The Canvas has three conversion functions for translating          between the world and canvas coordinate systems. The basic          functions, gnome_canvas_w2c( ) and gnome_canvas_c2w( ), take          two gdouble parameters for the world coordinates and two int          parameters for the Canvas coordinates.  If you want to use          gdouble parameters for both systems, you can use the          gnome_canvas_w2c_d( ) function instead. These functions work          similarly to the item-based conversion functions in the          previous section, except that because the two systems have          different types, it's not possible to pass the input and          output values through the same parameters. Instead, you pass          each coordinate separately:        </P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gnome_canvas_w2c(GnomeCanvas *canvas,    double wx, double wy, int *cx, int *cy);void gnome_canvas_c2w(GnomeCanvas *canvas,    int cx, int cy, double *wx, double *wy);void gnome_canvas_w2c_d(GnomeCanvas *canvas,    double wx, double wy, double *cx, double *cy);        </PRE></TD></TR></TABLE><P>          You can also use the affine transform. If you need to          reverse it to obtain the c2w transform, you can call          art_affine_invert( ), as discussed in Section 10.4.5.        </P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gnome_canvas_w2c_affine(GnomeCanvas *canvas,    double affine[6]);        </PRE></TD></TR></TABLE></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN1072">Window Coordinates</A></H2><P>          Window coordinates, the final coordinate system of interest          to Canvas users, are rarely used within the Canvas because          most internal Canvas calculations use world          coordinates. Instead window coordinates are used usually          outside of the Canvas. Like canvas coordinates, window          coordinates are pixel based, but they are relative to the          upper left corner of the top-level window that owns the          Canvas, rather than to the upper left corner of the Canvas          widget.        </P><P>          The only time you should need to translate to or from window          coordinates is to process GDK events not already handled by          the Canvas. For example, if you wanted to support          drag-and-drop from external applications into your Canvas,          you would have to process those events yourself. The          coordinates passed inside the GDK events would be in window          coordinates; you would need to transform them into native          world coordinates before using them inside the Canvas          because all the Canvas items store their own positions as          world coordinates. This is exactly what the Canvas does          internally when it's processing mouse and keyboard events.        </P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gnome_canvas_window_to_world(GnomeCanvas *canvas,    double winx, double winy, double *worldx, double *worldy);void gnome_canvas_world_to_window(GnomeCanvas *canvas,    double worldx, double worldy, double *winx, double *winy);        </PRE></TD></TR></TABLE></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="gnome-canvas.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="gnome-canvas-using.html">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">The GNOME Canvas</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="gnome-canvas.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Using the Canvas</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?