[Openmcl-devel] XFillPolygon

Rainer Joswig joswig at lisp.de
Sun Mar 7 15:02:46 PST 2010


Am 07.03.2010 um 17:27 schrieb Ron Garret:

> I don't see any reason to make this a macro instead of a function.

Right.

Also the macro has a few problems:

* points will be evaluated more than once

* variables i and npoints are introduced. That can be a problem. Example:


(loop for i from 10 upto 20
  (x/fillpolygon (mypoints foo i)))

The first use of points is okay.

(let ((i 0) (npoints (length (mypoints foo i))))
  ...

but the second shows the problem

  (dolist (p (mypoints foo i))

    ....))

But that's the 'i' of the LET, which is always iterating from 0.

Or try to expand this:

(x/fillpolygon xpoints)


Macros should not introduce these kinds of variables and there are several variables introduced in the macro - not only the one from the LET. One needs to create symbols that do not clash with application symbols. Otherwise one can create bugs that are extremely hard to find.

But best listen to Ron and implement it as a function. Remember, one can also declare a function to be inlined.

Regards,

Rainer Joswig



> 
> rg
> 
> On Mar 7, 2010, at 2:42 AM, Taoufik Dachraoui wrote:
> 
>> I found what is wrong in the code to fill a polygon, actually I had many errors in the code and I corrected it as follows:
>> 
>> (defmacro x/fillpolygon (points &key (shape #$Complex) (mode #$CoordModeOrigin))
>> "points is a list of pairs, shape is one of Complex, Convex,  or Nonconvex, and
>> mode is one of CoordModeOrigin or CoordModePrevious."
>>     `(let ((i 0) (npoints (length ,points)))
>>        (ccl::%stack-block ((xpoints (* (ccl::foreign-size :<XP>oint :bytes)
>> 				       (1+ npoints))))
>> 	 (dolist (p ,points)
>> 	   (rlet ((xpoint :<XP>oint))
>> 		 (setf (pref xpoint :<XP>oint.x) (car p))
>> 		 (setf (pref xpoint :<XP>oint.y) (cadr p))
>> 		 (setf (paref xpoints (:* :<XP>oint) i) xpoint)
>> 		 (incf i)))
>> 	 (#_XFillPolygon *display* 
>> 			 (window *context*)
>> 			 (graphics-context *context*)
>> 			 xpoints
>> 			 npoints
>> 			 ,shape
>> 			 ,mode
>> 			 ))))
>> 
>> 
>> Kind regards
>> Taoufik
>> 
>> On Sun, Mar 7, 2010 at 9:50 AM, Taoufik Dachraoui <taoufik at mazeboard.com> wrote:
>> Good Morning,
>> 
>> 
>> I am trying to write x/fillpolygon macro (see below) to call XFillPolygon as defined below:
>> 
>> XFillPolygon(display, d, gc, points, npoints, shape, mode)
>>       Display *display;
>>       Drawable d;
>>       GC gc;
>>       XPoint *points;
>>       int npoints;
>>       int shape; 
>>       int mode; 
>> 
>> 
>> (defmacro x/fillpolygon (points &key (shape #$Complex) (mode #$CoordModeOrigin))
>> "points is a list of pairs, shape is one of Complex, Convex,  or Nonconvex, and
>> mode is one of CoordModeOrigin or CoordModePrevious."
>>     `(rlet ((xpoints (:array :<XP>oint ,(length points))))
>>        (let ((i 0))
>> 	 (dolist (p ,points)
>> 	   (rlet ((xpoint :<XP>oint))
>> 		 (setf (pref xpoint :<XP>oint.x) (car p))
>> 		 (setf (pref xpoint :<XP>oint.y) (cdr p))
>> 		 (setf (paref xpoints (:array :<XP>oint ,(length points)) i) xpoint)
>> 		 (incf i)))
>> 	 (#_XFillPolygon *display* 
>> 			 (window *context*)
>> 			 (graphics-context *context*)
>> 			 xpoints
>> 			 ,(length points)
>> 			 ,shape
>> 			 ,mode
>> 			 ))))
>> 
>> 
>> I am not sure if I am correctly defining xpoints, what is the correct code to call #_XFillPolygon?
>> 
>> example of usage:
>> 
>> > (x/fillpolygon '((100 40) (140 50) (130 60)))
>> 
>> 
>> Kind regards
>> Taoufik
>> 
>> 
>> 



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.clozure.com/pipermail/openmcl-devel/attachments/20100308/ad06c90a/attachment.htm>


More information about the Openmcl-devel mailing list