/
sphere.c
70 lines (64 loc) · 1.34 KB
/
sphere.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
//
// sphere.c
//
//
// Created by Malika Aubakirova on 2/17/14.
//
//
#include <stdio.h>
#include "raytracer.h"
#include <stdlib.h>
sphere *sphere_new(vec *center, double radius, rgb *color)
{
sphere* news = malloc(sizeof(sphere));
if(news == NULL) {
printf("shpere_new: Cannot allocate\n");
exit (1);
}
news->center = vec_dup(center);
news->radius = radius;
news->color = rgb_dup(color);
return news;
}
sphere *sphere_dup(sphere *s)
{
sphere* news = malloc(sizeof(sphere));
if(news == NULL) {
printf("shpere_dup: Cannot allocate\n");
exit (1);
}
news->center = vec_dup(s->center);
news->radius = s->radius;
news->color = rgb_dup(s->color);
return news;
}
char *sphere_tos(sphere *s)
{
char buf[128];
int i;
for (i=0; i<127; i++)
buf[i] = '\0';
char *cen = vec_tos(s->center);
char *col= rgb_tos(s->color);
sprintf(buf, "Center: %s\nRadius: %lf\nColor: %s",
cen, s->radius, col);
free(cen);
free(col);
return strdup(buf);
}
void sphere_print(sphere *s)
{
printf("Center: ");
vec_print(s->center);
printf("Radius: %.2lf\n", s->radius);
printf("Color: ");
rgb_print(s->color);
return;
}
void sphere_free(sphere *s)
{
vec_free(s->center);
rgb_free(s->color);
free(s);
return;
}