Foxite.COM Community Weblog

Foxite.COM Community Weblog - free weblog service for the Visual FoxPro Community.
Welcome to Foxite.COM Community Weblog Sign in | Join | Help
in
Home Blogs Forum Photos Forum Archives

VFP IMAGING



PIE GRAPHICS WITH PURE VFP CODE

Did you know that it is possible to draw pie style graphics in VFP without the need of any Active-X or API call ? With pure VFP code ? draw a circle with foxpro

To draw any circle, there are two main parameters : 1 point with the coordinates of the center of the circle (x,y) and the Radius. Having this, it is very simple to calculate the coordinates of any point in the circumference border.

Even if you already use GDI+ or any  Active-X to draw graphics, I think it's imteresting to know how a circle is built, and how to calculate the positions of each point.

For this purpose, I'll need to make you remember the concepts of sine and cosine :

In any right angled triangle, for any angle:

Sine of the angle = length of the opposite side / length of the hypotenuse

Cosine of the angle = length of the adjacent side / length of the hypotenuse

Hypotenuse of a right angled triangle is the longest side, which is the one opposite the right angle. The adjacent side is the side which is between the angle in question and the right angle. The opposite side is opposite the angle in question.

 

So, imagine a right angled triangle inside a circumference, like in the picture below.

Sine of angle = opposite side (Height or Y) / hypotenuse (Radius) !!!

Height = Sine of angle * Radius

 

CoSine of angle = adjacent side (Width or X) / hypotenuse (radius) !!!

Width = CoSine of angle * Radius

Now we can create a loop starting from angle 0 (zero) and finishing at angle 360 degrees. At each step we can calculate the position of every point of the circle !

Then it becomes quite simple to draw the graphic. For that purpose, I'll use the line object, drawing lines from the center of the circle till the point X,Y just calculated. And that's all  !!!

There's one problem with the line object in VFP. To draw a line, we need to use the properties Top, Left, Width, Height and LineSlant (specifies which way a line slants, from upper left to lower right or lower left to upper right), instead of simply passing the two points, x1y1 and x2y2. So, as you can see in the code below, depending on the angle and the resulting position in the quadrant (quarter of the circumference of a circle), I needed to make some extra code to deal with this. Pay attention to the "DO CASE" command below.

PROCEDURE DRAWPIE

PARAMETERS tnCenterX, tnCentery, tnRadius, tnStart, tnEnd, tnColor

LOCAL lnLineWidth, n, x, y, lcObj, lnPointLeft, lnPointTop, lcSlant
lnLineWidth = 3

FOR n = tnStart TO tnEnd STEP (1 * lnLineWidth)
 x = COS(DTOR(n)) * tnRadius
 y = SIN(DTOR(n)) * tnRadius

 lcObj = "line" + TRANSFORM(n*100)
 Thisform.Container1.AddObject(lcObj,"line")

 DO CASE
 CASE
n >= 0 AND n < 90 && 1st Quadrant
  lnPointLeft = tnCenterX
  lnPointTop  = tnCenterY - y
  lcSlant = "/"

 CASE n >=90 AND n < 180 && 2nd Quadrant
  lnPointLeft = tnCenterx + x
  lnPointTop  = tnCenterY - y
  lcSlant = "\"

 CASE n >= 180 AND n < 270 && 3rd Quadrant
  lnPointLeft = tnCenterX + x
  lnPointTop  = tnCenterY
  lcSlant = "/" 
 
 CASE n >= 270 AND n <= 360 && 4th Quadrant
  lnPointLeft = tnCenterX
  lnPointTop  = tnCenterY
  lcSlant = "\"  
 ENDCASE

 WITH Thisform.Container1.&lcObj.
  .LineSlant = lcSlant
  .BorderColor = tnColor
  .BorderWidth = lnLineWidth
  .Width  = ABS(x)
  .Height = ABS(y)
  .left   = lnPointLeft
  .Top    = lnPointTop
  .Visible = .T.
 ENDWITH

ENDFOR
RETURN

You may find this procedure slow, specially if you run it in slow machines, but it works ! The biggest problem is that it adds many objects in a form, in the case of a loop with step 1, at least 360 lines !

If we draw a line with BorderWidth = 1, in some cases, the calculated points will create some holes between the lines. A wider border will resolve this problem. The bigger the step in the loop (from 0 to 360 degrees), the faster the procedure will run, and less objects will be added.

Run the form PieGraphics.scx from the attached file, change the cursor, radius, step, and linewidth values, and check all procedures to better understand this post.

What next ?

Of course, in one of my next posts I'll deal with Pie Style Graphics using GDI+

Some of the subjects discussed here will help us to create more interesting graphics.

As always, please send your comments, suggestions or fixes !

Published Friday, March 24, 2006 10:17 PM by cesarchalom
Filed Under:
Attachment(s): piegraphics.zip

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Rui Nogueira said:

Good example. What about the column percentage Bar Graphics?
March 27, 2006 5:59 PM
 

Luis Navas said:

You're doing a good job, all your blog entries are really good
March 28, 2006 5:29 PM
 

Malcolm Greene said:

Another great post! I've been enjoying your posts on GDI+ as well. Great stuff!
March 30, 2006 8:10 PM
 

Luis Maria Guayan said:

Good work! Taking the idea of graphs 100 % VFP, I have realized a class and an article (in Spanish)

Graphs with 100 % VFP objects
(Gráficas con objetos 100% VFP)
http://www.portalfox.com/article.php?sid=2211

There I mention to the entries of your blog
August 25, 2006 4:01 PM
 

Cesar Chalom said:

Gracias Luis Maria !
Great article, using some great ideas, with a great result.
I created the solution of published in this post 2 years ago, with VFP7, and is supposed to work with all VFP versions.
The approach from Luis Maria Guayan works only with VFP9, but the final result and performance from his solution is really impressive.
Thanks !
September 17, 2006 4:29 PM
 

Ana María Bisbé said:

Versión en Español de este artículo en / Spanish version at  http://www.portalfox.com/article.php?sid=2210
July 24, 2007 6:41 PM
 

Paul Mayo said:

An application called STAR Atlas-PRO (see http://www.skylab.com.au/ ) sells worldwide - it is a STAR ATLAS and draws charts of stars and galaxies - the application is written entirely in Visual Foxpro.

May 1, 2008 1:19 PM

What do you think?

(required) 
(optional)
(required)