How to resolve the algorithm Superellipse step by step in the C programming language
How to resolve the algorithm Superellipse step by step in the C programming language
Table of Contents
Problem Statement
A superellipse is a geometric figure defined as the set of all points (x, y) with
where n, a, and b are positive numbers.
Draw a superellipse with n = 2.5, and a = b = 200
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Superellipse step by step in the C programming language
The provided C program draws a superellipse using the Bresenham’s algorithm in graphics.h library. A super ellipse is a curve defined by the equation:
(x^n/a^n + y^n/b^n = 1, n > 0.
Where (a, b) are the lengths of the semi-major and semi-minor axes, respectively, and n determines the shape of the curve.
The program first includes the necessary header files and defines the mathematical constant pi.
It then prompts the user to enter the values of a, b, and n and stores them in the corresponding variables.
The program initializes a graphics window using the initwindow function and sets its size to 500x500 pixels.
It then enters a loop that iterates through the values of the angle i from 0 to 2*pi. For each value of i, it calculates the x and y coordinates of the point on the super ellipse using the Bresenham's algorithm.
Finally, it plots the point on the graphics window using the putpixel function and increments the angle i by a small value (incr).
The program continues to plot points until i reaches 2*pi, which completes one full cycle of the super ellipse.
After the loop is finished, the program displays a message indicating that the drawing is complete and then waits for the user to press any key to close the graphics window.
Source code in the c programming language
#include<graphics.h>
#include<stdio.h>
#include<math.h>
#define pi M_PI
int main(){
double a,b,n,i,incr = 0.0001;
printf("Enter major and minor axes of the SuperEllipse : ");
scanf("%lf%lf",&a,&b);
printf("Enter n : ");
scanf("%lf",&n);
initwindow(500,500,"Superellipse");
for(i=0;i<2*pi;i+=incr){
putpixel(250 + a*pow(fabs(cos(i)),2/n)*(pi/2<i && i<3*pi/2?-1:1),250 + b*pow(fabs(sin(i)),2/n)*(pi<i && i<2*pi?-1:1),15);
}
printf("Done. %lf",i);
getch();
closegraph();
}
You may also check:How to resolve the algorithm Anti-primes step by step in the Odin programming language
You may also check:How to resolve the algorithm Run-length encoding step by step in the Wren programming language
You may also check:How to resolve the algorithm N-queens problem step by step in the Ursala programming language
You may also check:How to resolve the algorithm Metronome step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Y combinator step by step in the PostScript programming language