c ++找到2点之间的距离

时间:2014-03-23 19:53:47

标签: c++

在我的测试程序中,我有两点,我想找到它们之间的距离。但我得到答案0.

  1. 为什么会给0?
  2. 我该如何解决?

    点和LT 2 - ; V1;   //这应该是{0.0,0.0}   点&LT 2 - ; v3 {list {2.0,3.0}};   float f = v1.distanceFrom(v3);   cout<< f<< ENDL;

  3. 我有一个point.h文件。

    #ifndef POINT_H
    #define POINT_H
    #include <iostream>
    #include <list>
    #include <sstream>
    #include <string>
    using std::stringstream;
    
    
    
    
    
    #include <cmath>
    
    
    using namespace std;
    
    template<unsigned short n>
    class Point {
    
    public:
        list <float> coords = {0.0};
    
        Point <n>() = default;
    
    
        Point <n>(list<float> coords){
            if (coords.size()!=n) {
                throw string ("Vale koordinaatide arv");
            }
            this-> coords=coords;
            }
    
    
    
            string toString(){
                string sone;
                ostringstream ss;
                sone.append("(");
                auto it3= coords.begin();
                while ((it3) != coords.end()){  
                    ss <<  (*it3);
                    sone.append(ss.str());
                    ss.str("");
                    sone.append(",");
                    ++it3;
                    }
                    sone.pop_back();
                sone.append(")");
                return sone;
            }
    
    
    
            float distanceFrom (Point <n> v){
                float s=0;
                list<float> coords;
                auto it1= coords.begin();
                auto it2= v.coords.begin();
                while ((it1) != coords.end()){  
                    s+=(*it1 -*it2)*(*it1-*it2);
                    it1++;
                    it2++;
                }
            return sqrt(s);
            }
        friend std::ostream& operator <<(std::ostream& out, const Point<n>& v)
        {
            out << "("<<"Test"<<")";
            return out;
        }
    };
    
    
    #endif
    

2 个答案:

答案 0 :(得分:0)

首先,您的coords列表不知道您希望其大小为n。默认初始化后的大小如下1

list <float> coords = {0.0};

构建它的正确方法是:

list <float> coords = list <float> (n, 0.0);

其次,您在函数coords中分配了一个新的distanceFrom

list<float> coords;

这会影响您实际想要使用的点的真实coords。删除该行,你会没事的。

答案 1 :(得分:0)

#include<iostream>
#include<cstdlib>
#include<cmath>

using namespace std;

class pointDistance{
    int x, y;
    public:
    pointDistance (int a, int b){
        x =a;
        y =b;
    }

    void pointShow(){
        cout<<"The Point is ("<<x<<","<<y<<")"<<endl;
    }
    friend void Distance(pointDistance , pointDistance);

};
//formula of distance between two points:
//d =((x1^2 - x2^2) + (y1^2 - y2^2))^1/2

void Distance(pointDistance o1, pointDistance o2)
{
    // pointDistance o3;
    int d1,d2;
    d1 = (o1.x -o2.x)*(o1.x -o2.x);
    d2 = (o1.y - o2.y)*(o1.y - o2.y);
    cout<<"So the distance between two point is "<< sqrt(d1+d2)<<endl;

}

int main(){
    pointDistance one(4,5);
    one.pointShow();

    pointDistance two(0,6);
    two.pointShow();

    Distance(one, two);
    

 return 0;
}