gdi tutorials_ circle-based shapes

8
 GDI T opics: Circl e-Based Shapes  Ellipses and Circles An elli pse is a closed continuous line whose points are positioned so tha t two po ints exac tly opposite eac h other have the exact same distant from a point called the center. It can be illustrated as follows: Becaus e an e llipse can fit i n a rectang le, in GDI progra mming , an ell ipse is defined with regards to a rectangle it would fit in. Therefore, to draw an ellipse, you specify its rectangular corners. The syntax used to do this is: voi d __fastca l l El l i pse( i nt X1, i nt Y1, i nt X 2, i nt Y2); The arguments of this method play the same roll as those of the Rectangle() method: Here is an example: //----------------- ----------------- ------------- ---------------------------- vo i d __f ast cal l TForm 1:: For m Pa i nt ( TO bj ect *Se nde r ) { C anvas- >El l i pse( 20, 20, 226, 144) ; } //----------------- ----------------- ------------- ---------------------------- Like the rectangle, you can draw an ellipse using a RECT or TRect object it would fit in. The syntax you would use is: Automate Java Testing Integrated code analysis, unit testing and regression testing alm.parasoft.com/Java_Testing Compare Excel tables Powerful and handy add-on for Excel 2000-2007 files comparison. www.office-excel.com GDI Tutorials: Circle-Based Shapes http://www.functionx.com/bcb/gdi/circleshapes.ht 1 of 8 6/12/2012 7:24 PM

Upload: shiwam-isrie

Post on 14-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: GDI Tutorials_ Circle-Based Shapes

7/30/2019 GDI Tutorials_ Circle-Based Shapes

http://slidepdf.com/reader/full/gdi-tutorials-circle-based-shapes 1/8

 

GDI Topics: Circle-Based Shapes

 Ellipses and Circles

An ellipse is a closed continuous line whose points arepositioned so that two points exactly opposite eachother have the exact same distant from a point calledthe center. It can be illustrated as follows:

Because an ellipse can fit in a rectangle, in GDI programming, an ell ipse is defined with regards to

a rectangle it would fit in. Therefore, to draw an ellipse, you specify its rectangular corners. Thesyntax used to do this is:

voi d __fastcal l El l i pse( i nt X1, i nt Y1, i nt X2, i nt Y2);

The arguments of this method play the same roll as those of the Rectangle() method:

Here is an example:

/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

voi d __f astcal l TForm1: : For mPai nt ( TObj ect *Sender ){Canvas- >El l i pse( 20, 20, 226, 144) ;

}/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Like the rectangle, you can draw an ellipse using a RECT or TRect object it would fit in. The syntaxyou would use is:

Automate Java TestingIntegrated code analysis, unit testing

and regression testingalm.parasoft.com/Java_Testing

Compare Exceltables

Powerful and handy

add-on for Excel2000-2007 files

comparison.www.office-excel.com

I Tutorials: Circle-Based Shapes http://www.functionx.com/bcb/gdi/circleshapes.ht

8 6/12/2012 7:24 PM

Page 2: GDI Tutorials_ Circle-Based Shapes

7/30/2019 GDI Tutorials_ Circle-Based Shapes

http://slidepdf.com/reader/full/gdi-tutorials-circle-based-shapes 2/8

voi d __f ast cal l El l i pse( TRect Rect) ;

Here is an example:

/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -voi d __f astcal l TForm1: : For mPai nt ( TObj ect *Sender ){

 TRect Rect o(328, 125, 28, 8) ;Canvas- >El l i pse(Rect o) ;

}/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

A circle is an ellipse whose all points have the same distance with regards to a central point.

Round Rectangles and Round SquaresA rectangle qualifies as round if its corners do not form straight angles but rounded corners. It can

be illustrated as follows:

To draw such a rectangle, you can use the TCanvas::RoundRect() method. Its syntax is:

voi d __f ast cal l RoundRect( i nt X1, i nt Y1, i nt X2, i nt Y2, i nt X3, i nt Y3) ;

When this member function executes, the rectangle is drawn from the (x1, y1) to the (x2, y2)

points. The corners are rounded by an ellipse whose width would be x3 and the ellipse's heightwould be x3.

Here is an example:

/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -voi d __f astcal l TForm1: : For mPai nt ( TObj ect *Sender ){

Canvas- >RoundRect ( 20, 20, 275, 188, 42, 38) ;}/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

I Tutorials: Circle-Based Shapes http://www.functionx.com/bcb/gdi/circleshapes.ht

8 6/12/2012 7:24 PM

Page 3: GDI Tutorials_ Circle-Based Shapes

7/30/2019 GDI Tutorials_ Circle-Based Shapes

http://slidepdf.com/reader/full/gdi-tutorials-circle-based-shapes 3/8

A round square is a square whose corners are rounded.

Pies 

A pie is a fraction of an ellipse delimited by two lines that span from the center of the ellipse to oneside each. It can be illustrated as follows:

To draw a pie, you can use the TCanvas::Pie() method whose syntax is:

voi d __fastcal l Pi e( i nt X1, i nt Y1, i nt X2, i nt Y2, i nt X3, i nt Y3, i nt X4, i nt Y4) ;

The (X1, Y1) point determines the upper-left corner of the rectangle in which the ellipse thatrepresents the pie fits. The (X2, Y2) point is the bottom-right corner of the rectangle.

The (X3, Y3) point specifies the starting corner of the pie in a default counterclockwise direction.

The (X4, Y4) point species the end point of the pie.

To complete the pie, a line is drawn from (X3, Y3) to the center and from the center to the (X4, Y4)points.

Here is an example:

/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -voi d __f astcal l TForm1: : For mPai nt ( TObj ect *Sender )

{ Canvas- >Pi e(40, 20, 226, 144, 155, 32, 202, 115) ;}/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 

 Arcs

An arc is a portion or segment of an ellipse. This means that an arc is a non-complete ellipse.Because an arc must confirm to the shape of an ellipse, it is defined as it fits in a rectangle and canbe illustrated as follows:

I Tutorials: Circle-Based Shapes http://www.functionx.com/bcb/gdi/circleshapes.ht

8 6/12/2012 7:24 PM

Page 4: GDI Tutorials_ Circle-Based Shapes

7/30/2019 GDI Tutorials_ Circle-Based Shapes

http://slidepdf.com/reader/full/gdi-tutorials-circle-based-shapes 4/8

To draw an arc, you can use the TCanvas::Arc() method whose syntax is:

voi d __f ast cal l Arc( i nt X1, i nt Y1, i nt X2, i nt Y2, i nt X3, i nt Y3, i nt X4, i nt Y4) ;

Besides the left (X1, Y1) and the right (X2, Y2) borders of the rectangle in which the arc would fit,an arc must specify where it starts and where it ends. The additional points are set as the (X3, Y3)

and (X4, Y4) points of the figure. Based on this, the above arc can be illustrated as follows:

Here is an example:

/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -voi d __f astcal l TForm1: : For mPai nt ( TObj ect *Sender ){

Canvas- >Ar c( 20, 20, 226, 144, 202, 115, 105, 32) ;}/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Besides the Arc() method, the Win32 library provides the ArcTo() function used to draw an arc.

Its syntax is as follows:

BOOL Ar cTo( HDC hdc,i nt nLef t Rect , i nt nTopRect , i nt nRi ght Rect , i nt nBot t omRect ,i nt nXRadi al 1, i nt nYRadi al 1, i nt nXRadi al 2, i nt nYRadi al 2) ;

The hdc argument is a handle to the canvas on which you want to draw.

This method uses the same arguments as Arc(). The difference is that while Arc() starts drawing

at (x3, y3), ArcTo() does not inherently control the drawing starting point. It refers to the currentpoint, exactly like the LineTo() (and the PolylineTo(). methods. Therefore, if you want to specify

where the drawing should start, you can call TCanvas::MoveTo() before ArcTo(). Here is anexample:

/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -voi d __f astcal l TForm1: : For mPai nt ( TObj ect *Sender ){

HDC hDC = Canvas- >Handl e;

 TRect Rect o(20, 20, 226, 144) ; TPoi nt Pt 1(202, 115) ; TPoi nt Pt 2(105, 32) ;

Canvas- >MoveTo( 207, 155) ;Ar cTo( hDC,Recto. Lef t , Rect o. Top, Recto. Wi dt h( ) , Recto. Hei ght ( ) ,Pt1.x, Pt1.y, Pt 2. x, Pt 2. y);

}/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

I Tutorials: Circle-Based Shapes http://www.functionx.com/bcb/gdi/circleshapes.ht

8 6/12/2012 7:24 PM

Page 5: GDI Tutorials_ Circle-Based Shapes

7/30/2019 GDI Tutorials_ Circle-Based Shapes

http://slidepdf.com/reader/full/gdi-tutorials-circle-based-shapes 5/8

 

The Arc's Direction

Here is and arc we drew earlier with a call to Arc():

/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -voi d __f astcal l TForm1: : For mPai nt ( TObj ect *Sender ){

Canvas- >Ar c( 20, 20, 226, 144, 202, 115, 105, 32) ;}/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

You may wonder why the arc is drawn to the right side of a vertical line that would cross the center

of the ellipse instead of the left. This is because the drawing of an arc is performed from right toleft or from bottom to up, in the opposite direction of the clock. This is known as the

counterclockwise direction. To control this orientation, the Win32 library provides theSetArcDirection() function. Its syntax is:

i nt SetAr cDi r ect i on(HDC hdc, i nt ArcDirection) ;

This function specifies the direction the TCanvas::Arc() method should follow from the starting to

the end points. The argument passed as  ArcDirection controls this orientation. It can have thefollowing values:

Value Orientation

AD_CLOCKWISE The figure is drawn clockwise

AD_COUNTERCLOCKWISEThe figure is drawn

counterclockwise

The default value of the direction is AD_COUNTERCLOCKWISE. Therefore, this would be used if you do not specify a direction. Here is an example that uses the same values as above with adifferent orientation:

/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -voi d __f astcal l TForm1: : For mPai nt ( TObj ect *Sender ){

HDC hDC = Canvas- >Handl e;

Set Ar cDi r ect i on(hDC, AD_CLOCKWI SE) ;

Canvas- >Ar c( 20, 20, 226, 144, 202, 115, 105, 32) ;Canvas- >Ar c( 20, 20, 226, 144, 202, 115, 105, 32) ;

}/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

I Tutorials: Circle-Based Shapes http://www.functionx.com/bcb/gdi/circleshapes.ht

8 6/12/2012 7:24 PM

Page 6: GDI Tutorials_ Circle-Based Shapes

7/30/2019 GDI Tutorials_ Circle-Based Shapes

http://slidepdf.com/reader/full/gdi-tutorials-circle-based-shapes 6/8

After calling SetArcDirection() and changing the previous direction, all drawings would use thenew direction to draw arcs using Arc() or ArcTo() and other figures (such as chords, ellipses, pies,

and rectangles). Here is an example:

/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -voi d __f astcal l TForm1: : For mPai nt ( TObj ect *Sender ){

HDC hDC = Canvas- >Handl e;

SetAr cDi r ect i on(hDC, AD_COUNTERCLOCKWI SE);

Canvas- >Ar c( 20, 20, 226, 144, 202, 115, 105, 32) ;Canvas- >Ar c( 10, 10, 250, 155, 240, 85, 24, 48);

}/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

If you want to change the direction, you must call SetArcDirection() with the desired value. Hereis an example:

/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -voi d __f astcal l TForm1: : For mPai nt ( TObj ect *Sender ){

HDC hDC = Canvas- >Handl e;

SetAr cDi r ect i on(hDC, AD_COUNTERCLOCKWI SE);Canvas- >Ar c( 20, 20, 226, 144, 202, 115, 105, 32) ;

Set Ar cDi r ect i on(hDC, AD_CLOCKWI SE) ;Canvas- >Ar c( 10, 10, 250, 155, 240, 85, 24, 48);

}/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

At any time, you can find out the current direction used. This is done by calling theGetArcDirection() function. Its syntax is:

i nt GetArcDi r ecti on( HDC hdc) ;

This function returns the current arc direction as AD_CLOCKWISE orAD_COUNTERCLOCKWISE.

 Angular Arcs

I Tutorials: Circle-Based Shapes http://www.functionx.com/bcb/gdi/circleshapes.ht

8 6/12/2012 7:24 PM

Page 7: GDI Tutorials_ Circle-Based Shapes

7/30/2019 GDI Tutorials_ Circle-Based Shapes

http://slidepdf.com/reader/full/gdi-tutorials-circle-based-shapes 7/8

You can (also) draw an arc using the AngleArc() function. Its syntax is:

BOOL Angl eArc( HDC hdc, i nt X , i nt Y , DWORD dwRadius,FLOAT eStartAngle, FLOAT eSweepAngle) ;

The hdc  argument represents a handle to the canvas on which you want to draw. This functiondraws a line and an arc connected. The arc is based on a circle and not an ellipse. This implies thatthe arc fits inside a square and not a rectangle. The circle that would be the base of the arc is

defined by its center located at C(X, Y) with a radius of  dwRadius. The arc starts at an angle of eStartAngle. The angle is based on the x axis and must be positive. That is, it must range from 0°

to 360°. If you want to specify an angle that is below the x axis, such as -15°, use 360º-15°=345°.The last argument, eSweepAngle, is the angular area covered by the arc.

The AngleArc() function does not control where it starts drawing. This means that it may start at

the origin, unless a previous call to MoveTo() specified the beginning of the drawing.

Here is an example:

/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -voi d __f astcal l TForm1: : For mPai nt ( TObj ect *Sender ){

HDC hDC = Canvas- >Handl e;

Canvas- >MoveTo( 52, 28) ;Angl eAr c( hDC, 120, 45, 142, 345, - 65) ;

}/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 

Chords

The arcs we have drawn so far are considered open figures because they are made of a line that

has a beginning and an end (unlike a circle or a rectangle that do not). A chord is an arc whose twoends are connected by a straight line. In other words, a chord is an ellipse that is divided by astraight line from one side to another:

To draw a chord, you can use the TCanvas::Chord() method. It is defined as follows:

voi d __f ast cal l Chord( i nt X1, i nt Y1, i nt X2, i nt Y2, i nt X3, i nt Y3, i nt X4, i nt Y4) ;

The  X1, Y1,  X2, and Y2 are the coordinates of the rectangle in which the chord of the circle wouldfit.

The  X3 and Y3 coordinates specify where the arc that holds the chord starts.

The  X4 and Y4 arguments specify the end of the arc.

To complete the chord, a line is drawn from ( X3, Y3) to ( X4, Y4).

Here is an example:

I Tutorials: Circle-Based Shapes http://www.functionx.com/bcb/gdi/circleshapes.ht

8 6/12/2012 7:24 PM

Page 8: GDI Tutorials_ Circle-Based Shapes

7/30/2019 GDI Tutorials_ Circle-Based Shapes

http://slidepdf.com/reader/full/gdi-tutorials-circle-based-shapes 8/8

/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -voi d __f ast cal l TForm1: : For mPai nt( TObj ect *Sender){

Canvas- >Chord( 20, 20, 226, 144, 202, 115, 105, 32) ;}/ / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Home Copyright © 2004-2007 FunctionX, Inc.

I Tutorials: Circle-Based Shapes http://www.functionx.com/bcb/gdi/circleshapes.ht