You are on page 1of 5

Graphics:

1.a)
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
int i,r,x,y,xc,yc;
float d;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\");
printf("Enter Radius\n");
scanf("%d",&r);
printf("Enter Center of circle\n");
scanf("%d",&xc);
scanf("%d",&yc);
d=1.25-r;
x=0;
y=r;
do
{
if(d<0)
{
x=x+1;
d=d+2*x+1;
}
else
{
x=x+1;
y=y-1;
d=d+2*x-2*y+10;
}
putpixel(xc+x,yc+y,5);
putpixel(xc-y,yc-x,5);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc+x,5);
putpixel(xc+y,yc+x,5);
putpixel(xc-x,yc-y,5);
putpixel(xc+x,yc-y,5);
putpixel(xc-x,yc+y,5);
}
while(x<y);
getch();
}

1.b)
#define _USE_MATH_DEFINES
#include <iostream>
#include <cmath>
#include <ctime>
#define DIST(a,b,c,d) (((a-c) * (a-c)) + ((b-d) * (b-d)))
#define RAND(min, max) (min + (((double)rand() / (double)RAND_MAX) * (max min)))
#define SAMPLES 10e6
using namespace std;
int main(int argsv, char *args[]) {
// Check arg count
if (argsv < 4) return 1;
// Read args
double x, y, u, v;
try {
x = atof(args[0]);
y = atof(args[1]);
u = atof(args[2]);
v = atof(args[3]);
}
catch (char *err) { return 1; }
// Check if circles overlap at all
if (DIST(x,y,u,v) >= 4.0) {
cout << M_PI * 2.0 << endl;
}
else
{
// Seed RNG
srand((unsigned int)time(NULL));
// Compute minimum bounding volume
double bx0 = (x < u ? x : u) - 1.0;
double bx1 = (x > u ? x : u) + 1.0;
double by0 = (y < v ? y : v) - 1.0;
double by1 = (y > v ? y : v) + 1.0;
double area = (bx1 - bx0) * (by1 - by0);
// Solve by monte carlo simulation
double result = 0.0;
for (unsigned int i = 0; i < SAMPLES; i++) {
float rx = RAND(bx0,bx1), ry = RAND(by0,by1);
float sample = (DIST(rx,ry,x,y) <= 1.0 ||
DIST(rx,ry,u,v) <= 1.0) ? 1.0 : 0.0;
}

result += sample;

// Multiply hit avg by bounding volume


result = (result / (double)SAMPLES) * area;
cout << result << endl;

return 0;
};

Programs:

#include
#include
#include
#include

<stdio.h>
<stdlib.h>
<string.h>
<math.h>

typedef struct {
size_t
width;
size_t
height;
unsigned char *data;
} Image;
static Image *
image_new (size_t width,
size_t height)
{
Image *image;
image = malloc (sizeof *image);
image->width = width;
image->height = height;
image->data = malloc (width * height);
}

return image;

static void
image_free (Image *image)
{
free (image->data);
free (image);
}
static void
image_fill (Image
*image,
unsigned char value)
{

memset (image->data, value, image->width * image->height);


}
/**
* image_set_pixel:
*
* Sets a pixel passed in signed (x, y) coordinates, where (0,0) is at
* the center of the image.
**/
static void
image_set_pixel (Image
*image,
ssize_t
x,
ssize_t
y,
unsigned char value)
{
size_t tx, ty;
unsigned char *p;
tx = (image->width / 2) + x;
ty = (image->height / 2) + y;
p = image->data + (ty * image->width) + tx;
}

*p = value;

static void
image_save (const Image *image,
const char *filename)
{
FILE *out;
out = fopen (filename, "wb");
if (!out)
return;
fprintf (out, "P5\n");
fprintf (out, "%zu %zu\n", image->width, image->height);
fprintf (out, "255\n");
fwrite (image->data, 1, image->width * image->height, out);
fclose (out);
}
static void
draw_circle (Image *image, int radius, unsigned char value);
int
main (int argc, char *argv[])
{
Image *image;
image = image_new (600, 600);
image_fill (image, 0xff);

draw_circle (image, 200, 0);


image_save (image, "circle.pgm");
image_free (image);
}

return 0;

You might also like