Search code examples
c++functioncomputational-geometry

How to find the computational geometry of circles and rectangles using x and y values in c++?


I'm a beginner in c++ and I've been tasked with finding certain geometries of circles and rectangles in c++. My issue is I don't know how to set up the parameters in either the header or source file, I don't want the solution, I want to better understand how to set up the functions.

Each function with its parameters are as follows:

  • GetCircumference(xc: double, yc: double, xe: double, ye: double): double

  • parameters:

    xc: double, x-value of circle’s center,

    yc: double, y-value of circle’s center,

    xe: double, x-value of point on circle’s edge, and

    ye: double, y-value of point on circle’s edge

– returns: the floating point value representing the circumference of a circle centered at (xc, yc) with a second point on the edge (xe, ye).

  • GetVolume(xc: double, yc: double, xe: double, ye: double): double
  • parameters:

    xc: double, x-value of circle’s center,

    yc: double, y-value of circle’s center,

    xe: double, x-value of point on circle’s edge, and

    ye: double, y-value of point on circle’s edge

– returns: the floating point value representing the volume of the circle centered at (xc, yc) with a second point on the edge (xe, ye).

  • GetPerimeter(xll: double, yll: double, xur: double, yur: double): double

  • parameters:

    xll: double, x-value of lower-left point of rectangle,

    yll: double, y-value of lower-left point of rectangle,

    xur: double, x-value of upper-right point of rectangle, and

    yur: double, y-value of upper-right point of rectangle

– returns: the floating point value representing the perimeter of the rectangle

  • GetDistanceSquared(x1: double, y1: double, x2: double, y2: double): double

  • parameters:

    x1: double, x-value of point 1,

    y1: double, y-value of point 1,

    x2: double, x-value of point 2, and

    y2: double, y-value of point 2

– returns: the floating point value representing the squared distance between points 1 and 2.

  • GetDistance(x1: double, y1: double, x2: double, y2: double): double

  • parameters:

    x1: double, x-value of point 1,

    y1: double, y-value of point 1,

    x2: double, x-value of point 2, and

    y2: double, y-value of point 2

– returns: the floating point value representing the distance between points 1 and 2.

comp_geo.h:

/*comp_geo.h*/
double GetCircumference(double xc, double yc, double xe, double ye);

double GetVolume(double, double, double, double);

double GetPerimeter(double, double, double, double);

double GetDistanceSquared(double, double, double, double);

double GetDistance(double, double, double, double);

comp_geo.cc:

/*comp_geo.cc*/
#include <cmath>

#include "comp_geo.h"

double GetCircumference(double xc, double yc, double xe, double ye) {
  double pi = 3.14159265358;
  double r = sqrt(pow((xe - xc), 2) + pow((ye - yc), 2));
  double c = 2 * pi * r;
  return c;
}

double GetVolume(double, double, double, double) {
  return 0.0;
}

double GetPerimeter(double, double, double, double) {
  return 0.0;
}

double GetDistanceSquared(double, double, double, double) {
  return 0.0;
}

double GetDistance(double, double, double, double) {
  return 0.0;
}

This is the test file that we were given to complete this assignment:

test.cc:

/*test.cc*/
#include <iostream>
using std::cout;
using std::endl;

#include "comp_geo.h"

bool TestGetCircumference() {
  const double expected = 0.0;
  double actual = GetCircumference(0.0, 0.0, 0.0, 0.0);
  if(actual != expected) {
    cout << "Expected: " << expected << ", Actual: " << actual << endl;
    return false;
  }

  return true;
}
bool TestGetPerimeter() {
  const double expected = 0.0;
  double actual = GetPerimeter(0.0, 0.0, 0.0, 0.0);
  if(actual != expected) {
    cout << "Expected: " << expected << ", Actual: " << actual << endl;
    return false;
  }

  return true;
}

bool TestGetDistanceSquared() {
  const double expected = 0.0;
  double actual = GetDistanceSquared(0.0, 0.0, 0.0, 0.0);
  if(actual != expected) {
    cout << "Expected: " << expected << ", Actual: " << actual << endl;
    return false;
  }

  return true;
}

bool TestGetDistance() {
  const double expected = 0.0;
  double actual = GetDistance(0.0, 0.0, 0.0, 0.0);
  if(actual != expected) {
    cout << "Expected: " << expected << ", Actual: " << actual << endl;
    return false;
  }

  return true;
}

int main(int argc, char* argv[]) {
  cout << "TestGetCircumference" << endl;
  if (!TestGetCircumference())
    return 1;
  cout << "TestGetPerimeter" << endl;
  if (!TestGetPerimeter())
    return 1;
  cout << "TestGetDistanceSquared" << endl;
  if (!TestGetDistanceSquared())
    return 1;
  cout << "TestGetDistance" << endl;
  if (!TestGetDistance())
    return 1;

  return 0;
}

and the makefile:

makefile:

CC = g++  # use the g++ compiler

FLAGS = -std=c++11  # compile with C++ 11 standard
FLAGS += -Wall      # compile with all warnings

LINK = $(CC) $(FLAGS) -o  # final linked build to binary executable

COMPILE = $(CC) $(FLAGS) -c  # compilation to intermediary .o files

test : comp_geo.o test.cc
  $(LINK) $@ $^

comp_geo.o : comp_geo.cc comp_geo.h
  $(COMPILE) $<

clean:
  @rm test comp_geo.o

Also, the way the grader is set up it will look for GetVolume but a circle does not have a volume so the professor suggested that we write a second function GetArea that calls and returns the value of the function GetVolume.


Solution

  • In a header:

    someType MyFunc(someType1 var1, someType2 var2); //notice the ";" at end
    

    someType is the type of what the function returns, for example double. Same goes for parameters. For example double MyFunc(int var1, double var2)

    In the source

    someType MyFunc(someType1 var1, someType2 var2)
    {
        do something with var1, var2
        return something of type 'someType'
    }