TweetFollow Us on Twitter

Graphics Objects
Volume Number:2
Issue Number:11
Column Tag:Lisp Listener

Simple Graphics Objects

By Andrew Shalit, Cambridge, MA

Graphics Objects in MacScheme

Welcome back to Lisp Listener! Our current choice of Lisp implementations is MacScheme by Semantic Microsystems. Scheme is a modern and elegant dialect of Lisp, and MacScheme is a robust, complete, and elegant implementation. Previously MacScheme did not offer pro-grammers much access to the Macintosh toolbox. In August a new release of a product called MacScheme + Toolsmith, was made which allows the production of event-driven programs. This will be followed shortly by an application builder that will make it possible to create stand-alone applications with MacScheme. The MacScheme editor, which is currently fairly limited, is also in the process of being upgraded. Look for reviews of all these items in future columns.

The only way to get to the toolbox in the previous incar-nation of MacScheme, version 1.11, was through an escape into machine language that looks very complicated (I haven't tried it out). Version 1.11 does, though, have some simple graphics capabilities which we will be using in this month's column. As it stands now, MacScheme is a great way to learn Lisp and explore object oriented programming.

Graphics in MacScheme

In early 1986, MacScheme was enhanced to allow the use of simple graphics operations. MacScheme has one graphics window, which must be opened with a procedure call before graphics can be used. The graphics window can be two sizes, 'full' or 'half'. To open up a small graphics window, the code would be:

 (start-graphics 'half)

Once a graphics window is open, you can get rid of it by saying:

 (end-graphics)

(One thing to be careful of: you aren't allowed to 'start-graphics' if the graphics window is already there, and you aren't allowed to end them if it isn't there. Moreover, there is currently no way for a procedure to test whether the graphics window is open. Hopefully Semantic Microsystems will add this test feature soon, or just allow you to open the window even if it already open, or close it if it is closed.)

Once the graphics window is open, there are just under 30 commands for drawing in it. These are all pretty basic: drawing, erasing, and inverting lines, rectangles, ovals, circles and points. You can also set up a picture that will be refreshed if the graphics window is obscured, draw a string, and clear the window.

For Macintosh programmers, MacScheme graphics take a little readjusting. This is because MacScheme uses the coordinate system that you learned in grade school, instead of the one you learned in Inside Macintosh. That's right, the x coordinate comes first, followed by the y coordinate. The half size graphics window is 470x130 pixels, so the procedure call

 (paint-oval 20 95 50 125)

would paint a circle in the lower left hand corner of the graphics window.

The general form of graphics procedures that work with two points is

(procedure  x1 y1 x2 y2)

Data Abstraction

This brings us to the first programming issue of the column: data abstraction. As you can imagine, it would be awkward working with rectangles and points if you always had to think about them in terms of their individual coordinates. One of the strong points of Lisp is its ability to create complex data objects. So, before I did anything with graphics, I created a set of procedures for working with points and rectangles. The simplest way to work with a rectangle is to set it up as a list of four coordinates. The coordinates can then be passed to a MacScheme graphics procedure by saying

 (apply the-procedure the-list )

In the sample procedures shown here, I use a slightly more complex data structure because it makes the issue of data abstraction stand out more clearly.

As I have defined them, a point is a simple pair, and a rectangle is a list of points. But the particular internal structure of a point or a rectangle is unimportant to most of the procedures I will write. When I want to work with a point or rectangle, I always do so with the selectors and constructors that I have created. I use a constructor to create a rectangle or point, and I use a selector to get information about a rectangle or point. The selectors and constructors are the only parts of the system that need to know what the internals of the data structure look like. Once you have a complete set of selectors and constructors, you can forget about the underlying structures which the selectors and constructors use to work with the data. This technique is called data-abstraction, and is very useful for keeping programs as simple as possible. For example, the procedure adds-points knows nothing about points besides the fact that they have an x and a y coordinate. If I change the way I store points, I need only modify the selectors and constructors; the rest of the program remains the same.

Now that we have a way of storing points and rectangles, we need a way of passing rectangles to Scheme graphics procedures. Because a rectangle is defined by two points (as all Macintosh programmers know), we can use the rectangle data form for any procedure that requires two points (i.e. four coordinates) as arguments. The result is the procedure 2-point-function. This procedure takes two arguments, a graphics procedure and a rectangle, and it calls the graphics procedure, giving it the coordinates from the rectangle as arguments. 2-point-function also illustrates the ease with which procedures can be passed as arguments in Lisp.

An additional feature of Lisp should be clear by now: Lisp programs are not constructed as single units, as are programs in other languages. Rather, procedures are defined, thereby adding to the procedures which come already defined in the language. A Lisp program is little more than the interaction of a number of procedures. The result is an extensible working environment, similar to that found in Forth.

Object oriented Programming

The next feature of Lisp we will discuss is the ease with which procedures can return other procedures. Every procedure in Lisp, when evaluated, returns something. For example, (+ 4 3) returns 7, and (car '(a b c)) returns a. In Lisp it is very easy to have a procedure return another procedure as its result. Here is a simple (though fairly useless) example, a procedure which churns out procedures to add a constant to a number.

(define (make-adder the-constant)
 (lambda (the-input-variable)
 (+the-constant
 the-input-variable)))
 

The procedure make-adder returns a procedure (a lambda expression) which takes a single argument, the-input-variable. If we say,

(set! addfive (make-adder 5))

we have a new procedure, called addfive, which will add 5 to any number it is given as an argument.

One of the most powerful features of Scheme is that it is lexically scoped. This means that variables within a procedure are scoped according to the environment in which the procedure is defined (as opposed to dynamic scoping, in which variables are scoped according to the environment from which the procedure is called ). In the example given above, the procedure addfive works because the variable 'the-constant' is scoped according to the environment in which addfive was defined. When addfive was defined, the-constant was equal to 5. As far as addfive is concerned, the-constant will always be 5, even if we call make-adder again and again, giving it a different number each time, and even if we call addfive from another procedure that has a variable called 'the-constant' with a different value.

When you put together lexical scoping and procedures returning procedures, you get the ability to do object oriented programming. In case you don't know it as more than a buzz-word, here's a brief description of object oriented programming.

In older forms of programming, data and procedures are stored separately. You have a bunch of data, and then you have the procedures that operate on the data. (What would Von Neuman have thought of this!?) In our rectangle example above, we would define a bunch of rectangles, and then we would have procedures that would do something to one or another of the rectangles. In object oriented programming the procedures and data are bundled together. Instead of having a procedure make a rectangle get bigger, you just send a message to the rectangle, telling it to make itself bigger. Or you tell the rectangle to move, or draw itself, or whatever. Because procedures in MacScheme are lexically scoped, they can have internal state. The internal state is the data within the object, and the rest of the procedure knows how to operate on this data. Object oriented programming has advantages that are similar to the advantages of data abstraction. Once you define an object, you can forget about how its insides work. You just treat it as a black box and work with it as a single unit. When you want it to do something, you tell it what to do; when you want to know something about it, you ask it. The creation of objects helps keep programs modular and simple. You work on small, easily understood units which you can then assemble into larger units, and so on.

The first objects we will be working with are ovals. You give the procedure make-oval a rectangle or any combination of points and coordinates, and it returns a procedure which is an object that can draw, erase, invert itself, tell you its bounding rectangle, or receive a new bounding rectangle. Because this object is a procedure, you call it just like you call any other procedure. The argument that you give it is called the 'message' which you send to the object. It is up to the object to decode the message and act accordingly, or signal an error if it doesn't know what to do.

The next stage of object oriented programming involves something called 'inheritance'. Inheritance occurs when one object takes on the characteristics and abilities of other objects, usually adding new abilities of its own. This month we will keep thing simple and just discuss single inheritance, that is, we will define an object that inherits from one other object.

When you call the procedure make-grow-oval, you give it a bounding rectangle as an argument. Make-grow-oval then sends this bounding rectangle to make-oval, and gets back an object, an oval. It then returns a new object, a grow-oval, which contains this recently (and completely locally) defined oval. When you send a message to a grow-oval it first checks to see if it recognizes the message, in which case it does the appropriate processing. If it doesn't recognize the message, it passes it directly to its oval (i.e. it lets it 'fall through' to the internal object). In this way a grow-oval can add new functionality to an oval without losing any of an oval's standard features. One other interesting thing to note: the grow-oval lets the oval take care of bookkeeping the current bounding rectangle. Whenever a grow-oval needs to know the bounding rectangle, it just asks its oval for the information.

Doing It Together

Anyone interested in learning Lisp should read Structure and Interpretation of Computer Programs by Hal Abelson and Gerald Sussman. This is not only a great book on computer programming, but it is all done in Scheme. The reference manual for MacScheme is also very well written, if you just want see what a particular command does. One other book on Scheme that Semantic Microsystems recommends is The Little Lispers, but I haven't seen it myself, and so I can't speak for it.

Graphics Objects in MacScheme 1.11 Program File

Andrew Shalit

3 Sacramento St.

Cambridge, MA 02138

(617) 498-6637

June 7, 1986


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;newgraphicsobjects
;;;a program that demonstrates graphics and
;;;object oriented programming in MacScheme 1.11
;;;copyright 1986, MacTutor Magazine
;;;written by Andrew Shalit
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;constructors for building points and rectangles
;;A point is a simple pair of coordinates : (x . y)
(define (make-point x . y)
 (if  (point? x)
 x
 (cons x (car y))))
;;a rectangle is a list of two points: ((x1 . y1) (x2 . y2))
(define (make-rect first-coord . other-coords)
 (if  (rectangle? first-coord)
 first-coord
 (let ((first-other (car other-coords)))
 (if  (point? first-coord)
 (list  first-coord
 (if  (point? first-other)
 first-other
 (apply make-point other-coords)))
 (apply make-rect
 (cons  (make-point
 first-coord first-other)
 (cdr other-coords)))))))

;selectors for getting coordinates out of points and rectangles
(define (x-coord point)
 (car point))
(define (y-coord point)
 (cdr point))
(define (left-top rectangle)
 (car rectangle))
(define (right-bottom rectangle)
 (cadr rectangle))
(define (left rectangle)
 (x-coord (left-top rectangle)))
(define (top rectangle)
 (y-coord (left-top rectangle)))
(define (right rectangle)
 (x-coord (right-bottom rectangle)))
(define (bottom rectangle)
 (y-coord (right-bottom rectangle)))

;;tests to determine whether something is a point or rectangle
(define (point? object)
 (if  (pair? object)
 (and (number? (car object))
 (number? (cdr object)))
 ()))
(define (rectangle? object)
 (if  (pair? object)
 (and (point? (car object))
 (point? (cadr object)))
 ()))

;functions for adding and subtracting points
(define (add-points point1 point2)
 (cons  (+ (x-coord point1) (x-coord point2))
 (+ (y-coord point1) (y-coord point2))))
(define (subtract-points point1 point2)
 (cons  (- (x-coord point1) (x-coord point2))
 (- (y-coord point1) (y-coord point2))))

;function for passing a rectangle to a graphics function
(define (2-point-function the-function the-rectangle)
 (the-function (left the-rectangle)
 (top the-rectangle)
 (right the-rectangle)
 (bottom the-rectangle)))

;;this is your basic oval that can draw, erase, invert itself,
;;tell its dimensions, and receive new dimensions
(define (make-oval . oval-definition)
 (let ((oval-definition (apply make-rect oval-definition)))
 (lambda (message)
 (if  (rectangle? message)
 (set! oval-definition message)
 (case message
 (DRAW (2-point-function paint-oval oval-definition))
 (ERASE (2-point-function erase-oval oval-definition))
 (INVERT (2-point-function invert-oval oval-definition))
 (DESCRIPTION oval-definition)
 (else (error "make-oval can't handle that definition"
 message)))))))

;;a grow-oval inherits all of the features of an oval, but can
;;also move and change size in more interesting ways
(define (make-grow-oval . oval-def)
 (let ((this-oval (apply make-oval oval-def)))
 (lambda (the-change . the-amount)
 (let ((old-description (this-oval 'description))
   (real-amount
 (if  the-amount
 (apply make-point the-amount))))
 (this-oval
 (case the-change
 (MOVE
 (make-rect
 (add-points
 real-amount
 (left-top old-description))
 (add-points
 real-amount
 (right-bottom old-description))))
 (MOVE-TO
 (make-rect
 real-amount
 (add-points
 real-amount
 (subtract-points
 (right-bottom 
 old-description)
 (left-top 
 old-description)))))
 (EXPAND
 (make-rect
 (subtract-points
 (left-top old-description) 
 real-amount)
 (add-points
 real-amount
 (right-bottom old-description))))
 (else the-change)))))))


;;;this procedure shows off some ovals
(define (oval-sampler)
 (let ( (oval-1 (make-grow-oval 5 5 50 50))
 (oval-2 (make-grow-oval 100 20 130 40))
 (oval-3 (make-grow-oval 30 90 60 120)))
 (clear-graphics)
 (oval-1 'draw)
 (oval-2 'draw)
 (oval-3 'draw)
 (oval-1 'move 5 5)
 (oval-1 'erase)
 (oval-2 'expand 4 4)
 (oval-2 'invert)
 (oval-3 'move-to 40 60 70 90)
 (oval-3 'draw)))
 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Fresh From the Land Down Under – The Tou...
After a two week hiatus, we are back with another episode of The TouchArcade Show. Eli is fresh off his trip to Australia, which according to him is very similar to America but more upside down. Also kangaroos all over. Other topics this week... | Read more »
TouchArcade Game of the Week: ‘Dungeon T...
I’m a little conflicted on this week’s pick. Pretty much everyone knows the legend of Dungeon Raid, the match-3 RPG hybrid that took the world by storm way back in 2011. Everyone at the time was obsessed with it, but for whatever reason the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for July 19th, 2024. In today’s article, we finish up the week with the unusual appearance of a review. I’ve spent my time with Hot Lap Racing, and I’m ready to give my verdict. After... | Read more »
Draknek Interview: Alan Hazelden on Thin...
Ever since I played my first release from Draknek & Friends years ago, I knew I wanted to sit down with Alan Hazelden and chat about the team, puzzle games, and much more. | Read more »
The Latest ‘Marvel Snap’ OTA Update Buff...
I don’t know about all of you, my fellow Marvel Snap (Free) players, but these days when I see a balance update I find myself clenching my… teeth and bracing for the impact to my decks. They’ve been pretty spicy of late, after all. How will the... | Read more »
‘Honkai Star Rail’ Version 2.4 “Finest D...
HoYoverse just announced the Honkai Star Rail (Free) version 2.4 “Finest Duel Under the Pristine Blue" update alongside a surprising collaboration. Honkai Star Rail 2.4 follows the 2.3 “Farewell, Penacony" update. Read about that here. | Read more »
‘Vampire Survivors+’ on Apple Arcade Wil...
Earlier this month, Apple revealed that poncle’s excellent Vampire Survivors+ () would be heading to Apple Arcade as a new App Store Great. I reached out to poncle to check in on the DLC for Vampire Survivors+ because only the first two DLCs were... | Read more »
Homerun Clash 2: Legends Derby opens for...
Since launching in 2018, Homerun Clash has performed admirably for HAEGIN, racking up 12 million players all eager to prove they could be the next baseball champions. Well, the title will soon be up for grabs again, as Homerun Clash 2: Legends... | Read more »
‘Neverness to Everness’ Is a Free To Pla...
Perfect World Games and Hotta Studio (Tower of Fantasy) announced a new free to play open world RPG in the form of Neverness to Everness a few days ago (via Gematsu). Neverness to Everness has an urban setting, and the two reveal trailers for it... | Read more »
Meditative Puzzler ‘Ouros’ Coming to iOS...
Ouros is a mediative puzzle game from developer Michael Kamm that launched on PC just a couple of months back, and today it has been revealed that the title is now heading to iOS and Android devices next month. Which is good news I say because this... | Read more »

Price Scanner via MacPrices.net

Amazon is still selling 16-inch MacBook Pros...
Prime Day in July is over, but Amazon is still selling 16-inch Apple MacBook Pros for $500-$600 off MSRP. Shipping is free. These are the lowest prices available this weekend for new 16″ Apple... Read more
Walmart continues to sell clearance 13-inch M...
Walmart continues to offer clearance, but new, Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBooks... Read more
Apple is offering steep discounts, up to $600...
Apple has standard-configuration 16″ M3 Max MacBook Pros available, Certified Refurbished, starting at $2969 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free,... Read more
Save up to $480 with these 14-inch M3 Pro/M3...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
Amazon has clearance 9th-generation WiFi iPad...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Apple is offering a $50 discount on 2nd-gener...
Apple has Certified Refurbished White and Midnight HomePods available for $249, Certified Refurbished. That’s $50 off MSRP and the lowest price currently available for a full-size Apple HomePod today... Read more
The latest MacBook Pro sale at Amazon: 16-inc...
Amazon is offering instant discounts on 16″ M3 Pro and 16″ M3 Max MacBook Pros ranging up to $400 off MSRP as part of their early July 4th sale. Shipping is free. These are the lowest prices... Read more
14-inch M3 Pro MacBook Pros with 36GB of RAM...
B&H Photo has 14″ M3 Pro MacBook Pros with 36GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 Pro MacBook Pro (... Read more
14-inch M3 MacBook Pros with 16GB of RAM on s...
B&H Photo has 14″ M3 MacBook Pros with 16GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $150-$200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 MacBook Pro (... Read more
Amazon is offering $170-$200 discounts on new...
Amazon is offering a $170-$200 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at $1129 for models with 8GB of RAM and 256GB of storage: – 15″ M3... Read more

Jobs Board

*Apple* Systems Engineer - Chenega Corporati...
…LLC,** a **Chenega Professional Services** ' company, is looking for a ** Apple Systems Engineer** to support the Information Technology Operations and Maintenance Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
*Apple* / Mac Administrator - JAMF Pro - Ame...
Amentum is seeking an ** Apple / Mac Administrator - JAMF Pro** to provide support with the Apple Ecosystem to include hardware and software to join our team and Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.