-
Notifications
You must be signed in to change notification settings - Fork 0
/
circle.c
83 lines (66 loc) · 2.04 KB
/
circle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
Author: Himol Shah
Description: Fill in a circle with the image
Details: Please link the mathematical library while compilation
use: "gcc circle.c -lm"
*/
// Header files
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <malloc.h>
#include <fcntl.h>
// Main Function
void main()
{
// Declaration of variables
int pix, npix, scan, nscan, *fp1,*fp2, x_centre, y_centre;
unsigned char *image;
char output_file[100], input_file[100];
float x, y, radi;
// accepting the dimensions of input image
printf("Enter npix nscan of the image: \n");
scanf("%d %d",&npix,&nscan);
image = (unsigned char *)calloc(npix,sizeof(unsigned char)); // allocating memory to the image array
// Accepting names of input and output files
printf("Enter name of the input file: \n");
scanf("%s", input_file);
printf("Enter name of the output file: \n");
scanf("%s",output_file);
// accepting radius of the circle
printf("Enter the radius of the circle: \n");
scanf("%f", &radi);
// computing centre coordinates
x_centre = (npix+1)/2;
y_centre = (nscan+1)/2;
fp1 = open(input_file,O_RDONLY); // opening the input file
if(fp1<0) // checking for errors while opening the file
{
printf("Error in opening the input file. \n");
exit(1);
}
fp2 = creat(output_file, 0666); // creating output file
if(fp2<0) // checking for errors while creating the file
{
printf("Error in creating the output file. \n");
exit(1);
}
for(scan = 0; scan < nscan; scan++)
{
read(fp1, &image[0], npix*sizeof(unsigned char));
for(pix = 0; pix < npix; pix++)
{
x = x_centre - pix;
y = y_centre - scan;
if(abs(sqrt((x*x + y*y))- radi) <= (float)(0.1)) //drawing boundaries of the circle
{
image[pix] = 0; // assigning black color to the boundaries
}
}
write(fp2, &image[0], npix*sizeof(unsigned char)); // write into the output file
}
free(image); // freeing memory
close(fp1); // closing file
close(fp2); // closing file
}