CMPT 130 Lab 10 - Circles

 

In this lab you are going to declare a struct that represents a circle and then write two functions that use your struct.

 

Labs are assessed, so make sure that Shruthi has seen, and marked, your finished work before you leave the lab.

 

Circle Struct

Declare a struct above the main function to represent a circle. Circles are to be represented by three floating point variables: two that represent the x and y coordinates of the centre of the circle and one that represents the circle's radius. Make sure you call it Circle, not circle or you will have to rewrite the main function.           

 

Intersection

Write a function called intersect that returns true if its two circle arguments intersect, and false otherwise.  The function prototype should look like this:

 

bool intersect(Circle c1, Circle c2)

 

Two circles intersect if the sum of their radii is less than or equal to the distance between them.  You can calculate the distance between two circles by computing the square root of the sum of the squares of the differences between their x and y coordinates.

 

distance = Ö( (difference between c1 and c2's x coordinates)2 + (difference between c1 and c2's y coordinates)2 )

 

Area

Write a function called area that returns the area of its circle argument.

 

float area(Circle c)

 

The area of a circle is equal to p * radius2.

 

Main Function

Test your functions with this main function.

 

#include <iostream>
#include <cmath>

using namespace std;

const double PI = 3.14159;

// Circle struct goes here

// Function prototypes

int intersect(Circle c1, Ccircle c2);

float area(Circle c);

 

int main()

{

       Circle c1 = { 5, 6, 3.2 };

       Circle c2 = { 6, 8, 1.2 };

 

       // Intersect test 1

       if (intersect(c1, c2)) {

              cout << "The circles intersect" << endl;

       }

       else {

              cout << "The circles do NOT intersect" << endl;

       }

 

       // Intersect test 2  

       Circle c3 = { 12, 2, 3.2 };

       Circle c4 = { 2, 12, 5.7 };

       if (intersect(c3, c4)) {

              cout << "The circles intersect" << endl;;

       }

       else {

              cout << "The circles do NOT intersect" << endl;

       }

 

       // Area test

cout << endl << "Area of c1 = " << area(c1) << endl << endl;

return 0;

}

 

Assessment

1 mark for completion of the lab.

 

 

CMPT 130 Home

 

John Edgar (johnwill@sfu.ca)