Use this link for reference Program 8 (Rectangles) Source and header files prog8
ID: 3713875 • Letter: U
Question
Use this link for reference
Program 8 (Rectangles) Source and header files
prog8_main.c http://mjgeiger.github.io/eece2160/programs/src/prog8_main.c
Point.h http://mjgeiger.github.io/eece2160/programs/src/Point.h
Point.c (starter file) http://mjgeiger.github.io/eece2160/programs/src/Point.c
Rectangle.h http://mjgeiger.github.io/eece2160/programs/src/Rectangle.h
Rectangle.c (starter file) Program 8 (Rectangles) Source and header files
prog8_main.c http://mjgeiger.github.io/eece2160/programs/src/prog8_main.c
Point.h http://mjgeiger.github.io/eece2160/programs/src/Point.h
Point.c (starter file) http://mjgeiger.github.io/eece2160/programs/src/Point.c
Rectangle.h http://mjgeiger.github.io/eece2160/programs/src/Rectangle.h
Rectangle.c (starter file) http://mjgeiger.github.io/eece2160/programs/src/Rectangle.c
Explanation / Answer
If you have any doubts, please give me comment...
Point.h
Rectangle.h
Point.c
#include "Point.h"
#include <stdio.h>
#include <math.h>
// Print coordinates as (x.xx, y.yy)
void printPoint(Point *p)
{
// printf("*** YOU MUST WRITE YOUR OWN VERSION OF printPoint() ***");
printf("(%.2lf %.2lf)", p->x, p->y);
}
// Read input coordinates
void readPoint(Point *p)
{
// printf("*** YOU MUST WRITE YOUR OWN VERSION OF readPoint() ***");
scanf("%lf %lf", &p->x, &p->y);
}
// Return distance between two points
double dist(Point *p1, Point *p2)
{
// printf("*** YOU MUST WRITE YOUR OWN VERSION OF dist() ***");
return sqrt((p2->x - p1->x) * (p2->x - p1->x) + (p2->y - p1->y) * (p2->y - p1->y));
return 0;
}
Rectangle.c
#include "Rectangle.h" // Implicitly includes Point.h
#include <stdio.h>
// Print contents of rectangle
// Prints vertices in appropriate relative positions:
// vert[1] vert[2]
// vert[0] vert[3]
void printRectangle(Rectangle *r)
{
printPoint(&r->vert[1]);
printPoint(&r->vert[2]);
printf(" ");
printPoint(&r->vert[0]);
printPoint(&r->vert[3]);
printf(" ");
// printf("*** YOU MUST WRITE YOUR OWN VERSION OF printRectangle() ");
// printf("(%lf %lf) (%lf %lf) ", r->vert[1].x, r->vert[1].y, r->vert[2].x, r->vert[2].y);
// printf("(%lf %lf) (%lf %lf) ", r->vert[0].x, r->vert[0].y, r->vert[3].x, r->vert[3].y);
}
// Print list of n Rectangles
void printList(Rectangle list[], int n)
{
// printf("*** YOU MUST WRITE YOUR OWN VERSION OF printList() ");
int i, j;
for (i = 0; i < n; i++){
printRectangle(&list[i]);
printf(" ");
}
}
// Returns area of rectangle
double area(Rectangle *r)
{
// printf("*** YOU MUST WRITE YOUR OWN VERSION OF area() ");
double l = r->vert[3].x - r->vert[0].x;
double w = r->vert[1].y - r->vert[0].y;
return l*w;
}
// Returns perimeter of rectangle
double perimeter(Rectangle *r)
{
// printf("*** YOU MUST WRITE YOUR OWN VERSION OF perimeter() ");
double l = r->vert[3].x - r->vert[0].x;
double w = r->vert[1].y - r->vert[0].y;
return 2*(l+w);
}
// Returns 1 if two rectangles overlap; 0 otherwise
int overlap(Rectangle *r1, Rectangle *r2)
{
// printf("*** YOU MUST WRITE YOUR OWN VERSION OF overlap() ");
if(r1->vert[1].x > r2->vert[3].x || r2->vert[1].x > r1->vert[3].x)
return 0;
if(r1->vert[1].y > r2->vert[3].y || r2->vert[1].y > r1->vert[3].y)
return 0;
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.