Showing posts with label computer graphics. Show all posts
Showing posts with label computer graphics. Show all posts

## Wednesday, 12 June 2013

### Simple Pie Chart Implementation In Turbo C

This is a very simple implementation of pie chart in Turbo C. The source code is quite easy to read through so must not be the big problem understanding the code.

This implementation makes use of pieslice() function which draws and fills the pie slice with centre x, y and radius r. The function also requires the start angle and end angle.

```#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>

#define MAX 20
#define X_CENTRE getmaxx()/2
#define Y_CENTRE getmaxy()/2

struct pie_data
{
char desc;
int freq;
int color;
int style;
float angle;
};

int main()
{
struct pie_data data[MAX];
int gd = DETECT, gm;
int i, j, k, n, total_freq = 0, start_angle = 0, end_angle = 0, xmax, ystart = 80, yend;
printf("Enter the number of items: ");
scanf("%d", &n);

for (i = 0, j = 1, k = 1; i < n; i++)
{
printf("Enter the item title: ");
scanf("%s", data[i].desc);
printf("Enter the item frequency: ");
scanf("%d", &data[i].freq);
total_freq += data[i].freq;
data[i].color = j;
data[i].style = k;
if (j++ >= 13) j = 1;
if (k++ >= 11) k = 1;
}

for (i = 0; i < n; i++)
{
float angle;
data[i].angle = 360 * (data[i].freq /(float) total_freq);
}

initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
xmax = getmaxx() - 150;
setaspectratio(10000, 10000);
for (i = 0; i < n; i++)
{
end_angle = start_angle + data[i].angle;
setcolor(data[i].color);
setfillstyle(data[i].style, data[i].color);
start_angle = end_angle;
yend = ystart + 40;
bar(xmax, ystart, xmax + 70, yend);
ystart = yend + 15;
outtextxy(xmax + 80, ystart - 20, data[i].desc);
}
getch();
closegraph();
return 0;
}
```

Below is the screenshot for sample run: ## Sunday, 31 March 2013

### Implementation Of BLA Line Drawing Algorithm

Here is the source code that makes use of the traditional DOS mode graphics to implement the Bresenham line drawing algorithm for the lines with slope |m| < 1.

### Bresenham Line Drawing Algorithm for |m| < 1

#### Algorithm

1) Input two points (x1, y1) & (x2, y2).
2) Determine the differences dx = x2 - x1 and dy = y2 - y1.
3) Calculate the initial decision parameter P0 = 2dy - dx.
4) For each xk along the line starting at k = 0,
if Pk < 0,
a) put a pixel at (xk + 1, yk)
b) Pk+1 = Pk + 2dy
else
a) put a pixel at (xk + 1, yk + 1)
b) Pk+1 = Pk + 2dy - 2dx.
5) Repeat step 4 for dx time.
6) End

#### Source Code

```#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>

int main()
{
int gd = DETECT, gm;
int x1, y1, x2, y2, dx, dy;
int x, y, i, p0, pk;
printf("Enter x1, y1: ");
scanf("%d %d", &x1, &y1);
printf("Enter x2, y2: ");
scanf("%d %d", &x2, &y2);

dx = x2 - x1;
dy = y2 - y1;

x = x1;
y = y1;
p0 = ( 2 * dy - dx);
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
pk = p0;
for (i = 0; i < abs(dx); i++)
{
if (pk < 0)
{
putpixel(x, y, WHITE);
pk += (2 * dy);
}
else
{
putpixel(x, y, WHITE);
pk += (2 * dy - 2 * dx);
}

(x1 < x2)?x++:x--;
(y1 < y2)?y++:y--;

delay(50);
}

getch();
closegraph();
return 0;
}
```

Make sure to provide an appropriate path for graphics library.

## Tuesday, 26 March 2013

### Implementation of DDA Line Drawing Algorithm

Here is a source code in traditional Turbo C that makes use of old DOS mode graphics to implement the digital differential analyzer.

### Digital Differential Analyzer

#### Algorithm

1) Input two points (x1, y1) & (x2, y2).
2) Determine the differences dx = x2 - x1 and dy = y2 - y1.
3) Choose step size as the bigger value between the absolute values of dx and dy.
4) Determine x-increment = dx/step_size and y-increment = dy/step_size.
5) Start from (x0, y0) = (x1, y1).
6) For i -> 0 to stepsize:
a) draw pixel at (xi, yi)
b) set xk = xk + x-increment
b) set yk = yk + y-increment

#### Source Code

```#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>

int main()
{
int gd = DETECT, gm;
int x1, y1, x2, y2, dx, dy, stepsize;
float xinc, yinc, x, y;
int i;
printf("Enter x1, y1: ");
scanf("%d %d", &x1, &y1);
printf("Enter x2, y2: ");
scanf("%d %d", &x2, &y2);
dx = x2 - x1;
dy = y2 - y1;
stepsize = (abs(dx) > abs(dy))?abs(dx):abs(dy);
xinc = dx/(float)stepsize;
yinc = dy/(float)stepsize;
x = x1;
y = y1;
initgraph(&gd, &gm, "C:\\TC\\BGI");
putpixel(x, y, WHITE);
delay(10);
for (i = 0; i < stepsize; i++)
{
x += xinc;
y += yinc;
putpixel(x, y, WHITE);
delay(50);
}
getch();
closegraph();
return 0;
}
```

Make sure to provide an appropriate path for graphics library.
{"ft_lang_label":"__label__en","ft_lang_prob":0.5568633,"math_prob":0.99862075,"size":6083,"snap":"2020-45-2020-50","text_gpt3_token_len":2076,"char_repetition_ratio":0.09343642,"word_repetition_ratio":0.5428832,"special_character_ratio":0.37481505,"punctuation_ratio":0.21542941,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9979695,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-11-24T11:57:15Z\",\"WARC-Record-ID\":\"<urn:uuid:ca4144df-6cc0-4be7-8b5b-4b575793128b>\",\"Content-Length\":\"156176\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7c70c62c-0c65-4a59-a2e0-7bb55fb5a250>\",\"WARC-Concurrent-To\":\"<urn:uuid:01b2429c-bcaa-47ec-8834-cde9973a0aac>\",\"WARC-IP-Address\":\"172.217.7.179\",\"WARC-Target-URI\":\"http://www.techgaun.com/search/label/computer%20graphics?max-results=8\",\"WARC-Payload-Digest\":\"sha1:LLGETF7JO5PHCHYJWYKMR7FCMODWSNAY\",\"WARC-Block-Digest\":\"sha1:RHDCRHJJT47XE4E7QWDHZ37GRTV7XPQV\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-50/CC-MAIN-2020-50_segments_1606141176256.21_warc_CC-MAIN-20201124111924-20201124141924-00645.warc.gz\"}"}