Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Graphic Objects


Fancier Graphic Objects

Listing Six contains definitions of four more complicated types of graphic objects. The point differs slightly from simple in that it expects the object to which it's attached to contain a list of at least two numbers. These become the x and y coordinates of the point. Gobs like this one are useful simulations of movement when you put several in one window.

(defgob 'point
  #'nilfn
  #.'(lambda (val window scale)
     (point window
       (* (car val) scale) (* (cadr val) scale))))
       
(setq dialmin 0 dialmax 100)

(defgob 'dial
   #'(lambda (val window scale)
   (circle window
      (* 50 scale) (* 50 scale)
      (* 48 scale)))
   #'(lambda (val window scale)
     (let ((theta (+ pi (' pi (div (- val dialmin)
                                   (- dialmax dialmin)))))
     (r (* 48 scale))
     (x1 (* 50 scale))
     (y1 (* 50 scale)))

     (line window
           x1 y1
           (+ x1 (* r (cos theta)))
           (+ y1 (* r (sin theta)))))))
(defgob 'function
   #'nilfn
   #'(lambda (vals window scale)
      (plotcurve window
           (sort vals #'(lambda (x y)
                     (< (car x) (car y))))
           scale)))

(defgob 'parametric
   #'nilfn
   #'(lambda (vals window scale)
   (plotcurve window vals scale)))

(defun plotcurve (window points scale)
   (unless < (length points) 2)
   (let ((p1 (car points)) (p2 (cadr points)))
      (line window
         (* (car p1) scale)
         (* (- 100 (cadr p1)) scale)
         (* (car p2) scale)
         (* (- 100 (cadr p2)) scale)))
(plotcurve window (cdr points) scale)))

Listing Six: Graphic object.

The second gob in Listing Six is the long-awaited dial. This version is reduced to its essentials, consisting only of a circle and a line. In actual use, dials have sort of minimum and maximum range -- here, dialmin and dialmax are by first set to 0 and 100, repectively. This range occupies the top 180 degrees of the dial, a situation easily altered by changing the culation of theta.

Finally, note that the dial, like all gobs for which size is an issue, is designed to fit by default into a 100-x-100 square, so that's how big it will be with scale 1.

The remaining gobs, function and parametric, assume that they are attached to an entry containing a list of (x,y) pairs. The only difference between the two is the order in which they connect these points: A function connects the order of increasing x coordinates; and a parametric connects them in the order in which they occur. Gobs like these take only slightly more effort to make than the simple gob, but a whole screen full of them working at runtime is a striking sight.

The graphic objects we've seen are really very simple. The point of this article is that making more complex ones does not mean starting over from scratch. When an interface is written in the style presented here, making it more sophisticated is largely a process of elaboration.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.