Computer Graphics
Graphic Systems
Input-Output Devices
Scan Conversion a line
Scan Conversion Circle
Scan Converting Ellipse
Filled Area Primitives
2D Transformations
2D-Viewing
Clipping Techniques
Pointing & Positioning
3D Computer Graphics
Hidden Surfaces
Projection
Programs
Trignometric Method
The following equation defines an ellipse trigonometrically as shown in fig:
x = a * cos (θ) +h and
y = b * sin (θ)+k
where (x, y) = the current coordinates
a = length of major axis
b = length of minor axis
θ= current angle
(h, k) = ellipse center
In this method, the value of θ is varied from 0 to radians. The remaining points are found by symmetry.
Exception Handling in Java - Javatpoint
Drawback:
- This is an inefficient method.
- It is not an interactive method for generating ellipse.
- The table is required to see the trigonometric value.
- Memory is required to store the value of θ.
Algorithm:
Step1: Start Algorithm
Step2: Declare variable x1,y1,aa1,bb1,aa2,bb2,fx,fy,p1,a1,b1
Step3: Initialize x1=0 and y1=b/* values of starting point of circle */
Step4: Calculate aa1=a1*a1
Calculate bb1=b1* b1
Calculate aa2=aa1*2
Calculate bb2=bb1*2
Step5: Initialize fx = 0
Step6: Initialize fy = aa_2* b1
Step7: Calculate the value of p1and round if it is integer
p1=bb1-aa1* b1+0.25* aa1/
Step8:
While (fx < fy) { Set pixel (x1,y1) Increment x i.e., x = x + 1 Calculate fx = fx + bb2 If (p1 < 0) Calculate p1 = p1 + fx + bb1/ else { Decrement y i.e., y = y-1 Calculate fy = fy - 992; p1=p1 + fx + bb1-fy } }
Step9: Setpixel (x1,y1)
Step10: Calculate p1=bb1 (x+.5)(x+.5)+aa(y-1)(y-1)-aa1*bb1
Step 11:
While (y1>0) { Decrement y i.e., y = y-1 fy=fx-aa2/ if (p1>=0) p1=p1 - fx + aa1/ else { Increment x i.e., x = x + 1 fx= fx+bb_2 p1=p1+fx-fy-aa1 } } Set pixel (x1,y1)
Step12: Stop Algorithm
Program to draw a circle using Trigonometric method:
- #include <graphics.h>
- #include <stdlib.h>
- #include <math.h>
- #include <stdio.h>
- #include <conio.h>
- #include <iostream.h>
- # define pi 3.14
- class bresen
- {
- float a, b, h, k, thetaend,step,x,y;
- int i;
- public:
- void get ();
- void cal ();
- };
- void main ()
- {
- bresen b;
- b.get ();
- b.cal ();
- getch ();
- }
- void bresen :: get ()
- {
- cout<<"\n ENTER CENTER OF ELLIPSE";
- cin>>h>>k;
- cout<<"\n ENTER LENGTH OF MAJOR AND MINOR AXIS";
- cin>>a>>b;
- cout<<"\n ENTER STEP SIZE";
- cin>> step;
- }
- void bresen ::cal ()
- {
- /* request auto detection */
- int gdriver = DETECT,gmode, errorcode;
- int midx, midy, i;
- /* initialize graphics and local variables */
- initgraph (&gdriver, &gmode, " ");
- /* read result of initialization */
- errorcode = graphresult ();
- if (errorcode ! = grOK) /*an error occurred */
- {
- printf("Graphics error: %s \n", grapherrormsg (errorcode);
- printf ("Press any key to halt:");
- getch ();
- exit (1); /* terminate with an error code */
- }
- theta= 0;
- thetaend=(pi*90)/180;
- whilex (theta<thetaend)
- {
- x = a * cos (theta);
- y = b * sin (theta);
- putpixel (x+h, y+k, RED);
- putpixel (-x+h, y+k, RED);
- putpixel (-x+h, -y+k, RED);
- putpixel (x+h, -y+k, RED);
- theta+=step;
- }
- getch();
- }
Output:
Ellipse Axis Rotation:
Since the ellipse shows four-way symmetry, it can easily be rotated. The new equation is found by trading a and b, the values which describe the major and minor axes. When the polynomial method is used, the equations used to describe the ellipse become
where (h, k) = ellipse center
a = length of the major axis
b = length of the minor axis
In the trigonometric method, the equations are
x = b cos (θ)+h and y=a sin(θ)+k
Where (x, y) = current coordinates
a = length of the major axis
b = length of the minor axis
θ = current angle
(h, k) = ellipse center
Assume that you would like to rotate the ellipse through an angle other than 90 degrees. The rotation of the ellipse may be accomplished by rotating the x &y axis α degrees.
x = a cos (0) - b sin (0+ ∞) + h y= b (sin 0) + a cos (0+∞) + k