找到坐标的中点 [英] Find midpoint of a coordinate

查看:118
本文介绍了找到坐标的中点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点表示x-y平面中的坐标。它由以下函数支持:



 Point * make_point(double x,double y)
double x_of(Point * p )
double y_of(Point * p)
void print_point(Point * p)



编写函数

 Point * mid_point 

接受两个点作为参数并返回一个点,这是这两个输入坐标的中点。



我是什么尝试过:



在网上simultor中用下面的表达式测试它但是无法正确

 mid_point(make_point(1.0, 1.0),make_point(3.0,3.0))





我的代码:

 Point * mid_point(Point * x,Point * y){

int mid;

mid = make_point((x_of(x)+ x_of(x))/ 2,(y_of(y)+ y_of(y))/ 2);

print_point(mid);

}

解决方案

您的语法错误:您需要通过 - >访问您的坐标带指针的运算符:

  double  x1 = x-> x_off; 
double y1 = x-> y_off;

我建议您更改函数参数的名称以减少混淆:

 Point * mid_point(Point * a,Point * b)
{
...
}


Quote:

Point * mid_point(Point * x,Point * y){< br $> b $ b

int mid;



mid = make_point((x_of(x)+ x_of(x))/ 2,(y_of(y)+ y_of(y))/ 2);



print_point(mid);



}





更改为

 Point * mid_point(Point * p,Point * q){

Point * m;

m = make_point((x_of(p)+ x_of(q))/ 2,(y_of(p)+ y_of(q))/ 2);

print_point(m);

返回m;
}


你需要实现一些功能,比如

  double  x_of(Point * p)
{
return p-> x;
}

但这错过了对空指针的错误处理所以

  double  x_of(Point * p)
{
return p? p-> x: 0 ;
}

应该是正确的。



当你分配内存时要注意内存管理你必须删除它。

学习使用调试器以及使用print或TRACE来完成作业。



搜索有关此语言详细信息的一些教程。


A point represents a coordinate in an x-y plane. It is supported by the following functions:

Point * make_point(double x, double y)
double x_of(Point *p)
double y_of(Point *p)
void print_point(Point *p)


Write a function

Point * mid_point

that accepts two points as arguments and returns a point that is the mid-point of these two input coordinates.

What I have tried:

Test it in online simultor with below expression but couldn't get it right

mid_point(make_point(1.0, 1.0), make_point(3.0, 3.0))



my code:

Point * mid_point(Point *x, Point *y) {
    
    int mid;

    mid = make_point((x_of (x) + x_of(x) ) / 2,  (y_of(y) + y_of(y) ) / 2);
    
    print_point(mid);
    
}

解决方案

Your syntax is wrong: you need to access your coordinates via the "->" operator with pointers:

double x1 = x->x_off;
double y1 = x->y_off;

I'd suggest that you change the names of the function parameters to reduce confusion:

Point* mid_point(Point* a, Point* b)
   {
   ...
   }


Quote:

Point * mid_point(Point *x, Point *y) {

int mid;

mid = make_point((x_of (x) + x_of(x) ) / 2, (y_of(y) + y_of(y) ) / 2);

print_point(mid);

}



Change to

Point * mid_point(Point *p, Point *q) {
    
    Point * m;

    m = make_point((x_of (p) + x_of(q) ) / 2,  (y_of(p) + y_of(q) ) / 2);
    
    print_point(m);

    return m;
}


You need to implement some functions like

double x_of(Point *p)
{
  return p->x;
}

but this misses error handling of null pointers so

double x_of(Point *p)
{
  return p ? p->x : 0;
}

should be correct.

And pay attention to memory managment when you allocate memory you must delete it.
Learn to use the debugger and the usage of print or TRACE for your homework.

Search for some tutorial on this language details.


这篇关于找到坐标的中点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆