如何在 C++ 中使用 x 和 y 值查找圆形和矩形的计算几何?

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

我是 c++ 的初学者,我的任务是在 c++ 中找到圆形和矩形的某些几何形状。我的问题是我不知道如何在头文件或源文件中设置参数,我不想要解决方案,我想更好地了解如何设置函数。

各函数及其参数如下:

– returns:表示以(xc,yc)为圆心的圆的周长的浮点值 在边缘 (xe, ye) 上有第二个点。

– returns: 浮点值表示以(xc, yc)为圆心,第二个点在(xe, ye)边上的圆的体积。

– returns:表示矩形周长的浮点值

– returns:表示点1和点2之间距离平方的浮点值。

– returns:表示点1和点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;
}

这是我们完成此作业的测试文件:

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;
}

和生成文件:

生成文件:

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

此外,评分器的设置方式会寻找 GetVolume,但圆没有体积,因此教授建议我们编写第二个函数 GetArea,调用和 returns函数的值GetVolume

在 header:

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

someType是什么函数的类型returns,比如double。参数也是如此。例如double MyFunc(int var1, double var2)

来源

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