Recent

Author Topic: [solved] defining some options :)  (Read 407 times)

speter

  • Sr. Member
  • ****
  • Posts: 337
[solved] defining some options :)
« on: May 20, 2022, 06:08:24 am »
G'Day Folks,

At present I have a bunch of (drawing) options in a program, when doing some drawing, I compare a variable to the options to decide whether to do a particular option. For example:

Code: Pascal  [Select][+][-]
  1. const
  2.  sz = 3;
  3.   // drawing options
  4.   oNotFill = 1;           // don't draw filled
  5.   oSqPts   = 2*oNotFill;  // draw points (as squares)
  6.   oCrPts   = 2*oSqPts;    // draw points (as circles)
  7.   oPts     = 2*oCrPts;    // draw shape's key points
  8.   oExtRect = 2*oPts;      // draw extents rectangle
  9.   oExtPts  = 2*oExtRect;  // draw extents' min/max pts
  10.   oCen     = 2*oExtPts;   // draw centre point (ellipse, regpoly, star, arc)
  11.  ... // up to 32 options
  12.  
  13. procedure draw(draw_options : cardinal; p1,p2 : tpoint);
  14. begin
  15. ...
  16.   if draw_options and oSqPts <> 0 then
  17.     begin
  18.       with p1 do fillrect(x-sz,y-sz, x+sz,y+sz);
  19.       with p2 do fillrect(x-sz,y-sz, x+sz,y+sz);
  20.     end;
  21. ...
  22. end;
  23. ...
  24.  
  25.   draw(oNotFill+oSqPts+oExtRect, point(100,100), point(200,200));

All this works fine; but, I am wondering though whether there might be a more elegant way to achieve this!?
In particular the const declarations seem quite messy. :)

Any advice / comments would be welcome!

cheers
S.
« Last Edit: May 20, 2022, 08:22:42 am by speter »
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Thausand

  • Sr. Member
  • ****
  • Posts: 292
Re: defining some options :)
« Reply #1 on: May 20, 2022, 07:00:03 am »
Set: https://www.freepascal.org/docs-html/ref/refsu16.html
Set operators: https://www.freepascal.org/docs-html/ref/refsu49.html#x156-18000012.8.6
wiki: https://wiki.freepascal.org/Set

Note in operator, e.g.
Code: Pascal  [Select][+][-]
  1. type
  2.  TOptions = (oSgPts, oCrPts, etc);
  3.  TDrawOptions = set of TOptions;
  4.  
  5. var
  6.   DrawOptions: TDrawOptions;
  7.  
  8. begin
  9.   ...
  10.   DrawOptions := [oSgpts];
  11.   ...
  12.  
  13.   if oSgPts in DrawOptions then
  14.   begin
  15.    ... DrawoSgPts ...
  16.   end;
  17.   ...
  18. end.
  19.  
« Last Edit: May 20, 2022, 07:02:16 am by Thausand »

speter

  • Sr. Member
  • ****
  • Posts: 337
Re: defining some options :)
« Reply #2 on: May 20, 2022, 08:22:19 am »
Thanks very much Thausand, that's exactly what I needed. :)

cheers
S.

PS: In case anyone else is looking into this kind of thing, I am posting a simple test app.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

 

TinyPortal © 2005-2018