Visual c++ 在c++;如何计算刀具路径长度?

Visual c++ 在c++;如何计算刀具路径长度?,visual-c++,Visual C++,我正在处理刀具轨迹的生成,其中包含许多三维点,我正在使用CNC机器生成它们。我想计算的一件事是刀具路径长度,它定义为路径的总长度。所以我试了一下: 1.6760 3.7901 6.1955 1.2788 4.1872 5.3681 0.2832 5.1828 3.2939 0.1835 5.2173 3.0576 0.1097 5.1205 2.8292 0.0815 4.9185 2.6699 0.0812 4.8728 2.6491 0.0810 4.8270 2.6288 0.080

我正在处理刀具轨迹的生成,其中包含许多三维点,我正在使用CNC机器生成它们。我想计算的一件事是刀具路径长度,它定义为路径的总长度。所以我试了一下:

1.6760 3.7901 6.1955 
1.2788 4.1872 5.3681
0.2832 5.1828 3.2939
0.1835 5.2173 3.0576
0.1097 5.1205 2.8292
0.0815 4.9185 2.6699
0.0812 4.8728 2.6491 
0.0810 4.8270 2.6288 
0.0807 4.7810 2.6089 
要点是这样的

// math.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
#include<math.h>

using std::cin;
using std::cout;
using std::endl;

using std::vector;

using std::ostream;
using std::istream;
using std::ifstream;

using std::operator>>;
using std::operator<<;

struct point

{
    float x ;
    float y ;
    float z ;
};

ostream& operator<< (ostream& out, const point &p)
{
    out << "(" << p.x << "," << p.y << " ," << p.z << "," << ")";
    return out;
}

istream& operator>> (istream& in, point& point)
{
    in >> point.x >> point.y >> point.z;
    return in;
}

struct line
{
    point start;
    point next;
    float sqDistance()
    {
        float dx = start.x - next.x;
        float dy = start.y - next.y;
        float dz = start.z - next.z;
        double distance = 0.0;
        distance = sqrt(dx * dx + dy * dy + dz * dz);
        return distance;
    }
};

ostream& operator<< (ostream& out, const line &ln)
{
    out << "From " << ln.start << " to " << ln.next;
    return out;
}

istream& operator>> (istream& in, line ln)
{
    cout << "Enter x y z start then x y z  next: ";
    in >> ln.start.x >> ln.start.y >> ln.start.z >>  ln.next.x >> ln.next.y >> ln.next.z;
    return in;
}

int main()
{
    point origin, input;
    line ray;
    vector<line> side;

    // READ POINTS FROM FILE
    ifstream pointfile("concave.txt");
    if (pointfile.is_open())
    {
        pointfile >> origin.x >> origin.y >> origin.z;
        cout << "origin: " << origin << endl;
        ray.start = origin;

        while (pointfile >> ray.next)
        {
            cout
                << " GOTO/ " << ray.next 
                << " The distance from point to the next is : "
                << ray.sqDistance() << endl;

            side.push_back(ray);
        }
    }
    else
        cout << "Unable to open file";
    pointfile.close();



    vector<line>::iterator iter = side.begin();
    line temp, closest = *iter;
    float minimumDistance = closest.sqDistance(), distance = 0.0;

    system("PAUSE");

    return 0;
}
//math.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括
#包括
#包括
#包括
使用std::cin;
使用std::cout;
使用std::endl;
使用std::vector;
使用std::ostream;
使用std::istream;
使用std::ifstream;
使用std::operator>>;
使用std::operator>ln.next.x>>ln.next.y>>ln.next.z;
返回;
}
int main()
{
点原点,输入;
线射线;
矢量侧;
//从文件中读取点
ifstream点文件(“凹面文件”);
if(pointfile.is_open())
{
点文件>>origin.x>>origin.y>>origin.z;

cout像这样的事情怎么样-假设您想要点之间的距离,而不是从起点开始(第93行带注释):

#包括“stdafx.h”
#包括
#包括
#包括
#包括
#包括
使用std::cin;
使用std::cout;
使用std::endl;
使用std::vector;
使用std::ostream;
使用std::istream;
使用std::ifstream;
使用std::operator>>;
使用std::operator>ln.next.x>>ln.next.y>>ln.next.z;
返回;
}
int main()
{
点原点,输入;
线射线;
矢量侧;
//从文件中读取点
ifstream点文件(“凹面文件”);
if(pointfile.is_open())
{
点文件>>origin.x>>origin.y>>origin.z;

cout像这样的事情怎么样-假设您想要点之间的距离,而不是从起点开始(第93行带注释):

#包括“stdafx.h”
#包括
#包括
#包括
#包括
#包括
使用std::cin;
使用std::cout;
使用std::endl;
使用std::vector;
使用std::ostream;
使用std::istream;
使用std::ifstream;
使用std::operator>>;
使用std::operator>ln.next.x>>ln.next.y>>ln.next.z;
返回;
}
int main()
{
点原点,输入;
线射线;
矢量侧;
//从文件中读取点
ifstream点文件(“凹面文件”);
if(pointfile.is_open())
{
点文件>>origin.x>>origin.y>>origin.z;

cout这里是完整的,略短的版本,包括simple results.txt文件输出。 您可以删除带有注释信息输出的两行:

#include <iostream>
#include <fstream>
#include <vector>
#include <conio.h>

using namespace std;

struct point
{
    float x;
    float y;
    float z;
};

ostream& operator<< (ostream& out, const point &p)
{
    out << "(" << p.x << "," << p.y << " ," << p.z << "," << ")";
    return out;
}

istream& operator>> (istream& in, point& point)
{
    in >> point.x >> point.y >> point.z;
    return in;
}

struct line
{
    point start;
    point next;
    double sqDistance()
    {
        float dx = start.x - next.x;
        float dy = start.y - next.y;
        float dz = start.z - next.z;
        double distance = 0.0;
        distance = sqrt(dx * dx + dy * dy + dz * dz);
        return distance;
    }
};

ostream& operator<< (ostream& out, const line &ln)
{
    out << "From " << ln.start << " to " << ln.next;
    return out;
}

istream& operator>> (istream& in, line ln)
{
    cout << "Enter x y z start then x y z  next: ";
    in >> ln.start.x >> ln.start.y >> ln.start.z >> ln.next.x >> ln.next.y >> ln.next.z;
    return in;
}

int main()
{
    point origin, input;
    line ray;
    vector<line> side;

    // READ POINTS FROM FILE
    ifstream pointfile("concave.txt");
    if (pointfile.is_open())
    {
        pointfile >> origin.x >> origin.y >> origin.z;
        cout << "origin: " << origin << endl;
        ray.start = origin;

        while (pointfile >> ray.next)
        {
            cout
                << " GOTO/ " << ray.next
                << " The distance from point to the next is : "
                << ray.sqDistance() << endl;

            side.push_back(ray);
            ray.start = ray.next; // set start to last end (?)
        }
    }
    else
        cout << "Unable to open file";
    pointfile.close();

    ofstream results("results.txt");
    vector<line>::iterator iter = side.begin();
    line closest = *iter;
    double distance, sumOfDistances = 0.0;
    cout << "Line coords" << endl << "distance | Sum of distances" << endl;
    while (iter != side.end()) {
        closest = *iter;
        distance = closest.sqDistance();
        sumOfDistances += distance;
        results << distance << endl;
        cout << closest << endl << distance << " | " << sumOfDistances << endl; // info output
        iter++;
    }
    results << sumOfDistances << " << Sum" << endl;
    results.close();
    cout << "Complete path distance: " << sumOfDistances << endl; // info output

    getch();

    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
结构点
{
浮动x;
浮动y;
浮动z;
};
ostream&operator>ln.start.z>>ln.next.x>>ln.next.y>>ln.next.z;
返回;
}
int main()
{
点原点,输入;
线射线;
矢量侧;
//从文件中读取点
ifstream点文件(“凹面文件”);
if(pointfile.is_open())
{
点文件>>origin.x>>origin.y>>origin.z;

cout这里是完整的,略短的版本,包括simple results.txt文件输出。 您可以删除带有注释信息输出的两行:

#include <iostream>
#include <fstream>
#include <vector>
#include <conio.h>

using namespace std;

struct point
{
    float x;
    float y;
    float z;
};

ostream& operator<< (ostream& out, const point &p)
{
    out << "(" << p.x << "," << p.y << " ," << p.z << "," << ")";
    return out;
}

istream& operator>> (istream& in, point& point)
{
    in >> point.x >> point.y >> point.z;
    return in;
}

struct line
{
    point start;
    point next;
    double sqDistance()
    {
        float dx = start.x - next.x;
        float dy = start.y - next.y;
        float dz = start.z - next.z;
        double distance = 0.0;
        distance = sqrt(dx * dx + dy * dy + dz * dz);
        return distance;
    }
};

ostream& operator<< (ostream& out, const line &ln)
{
    out << "From " << ln.start << " to " << ln.next;
    return out;
}

istream& operator>> (istream& in, line ln)
{
    cout << "Enter x y z start then x y z  next: ";
    in >> ln.start.x >> ln.start.y >> ln.start.z >> ln.next.x >> ln.next.y >> ln.next.z;
    return in;
}

int main()
{
    point origin, input;
    line ray;
    vector<line> side;

    // READ POINTS FROM FILE
    ifstream pointfile("concave.txt");
    if (pointfile.is_open())
    {
        pointfile >> origin.x >> origin.y >> origin.z;
        cout << "origin: " << origin << endl;
        ray.start = origin;

        while (pointfile >> ray.next)
        {
            cout
                << " GOTO/ " << ray.next
                << " The distance from point to the next is : "
                << ray.sqDistance() << endl;

            side.push_back(ray);
            ray.start = ray.next; // set start to last end (?)
        }
    }
    else
        cout << "Unable to open file";
    pointfile.close();

    ofstream results("results.txt");
    vector<line>::iterator iter = side.begin();
    line closest = *iter;
    double distance, sumOfDistances = 0.0;
    cout << "Line coords" << endl << "distance | Sum of distances" << endl;
    while (iter != side.end()) {
        closest = *iter;
        distance = closest.sqDistance();
        sumOfDistances += distance;
        results << distance << endl;
        cout << closest << endl << distance << " | " << sumOfDistances << endl; // info output
        iter++;
    }
    results << sumOfDistances << " << Sum" << endl;
    results.close();
    cout << "Complete path distance: " << sumOfDistances << endl; // info output

    getch();

    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
结构点
{
浮动x;
浮动y;
浮动z;
};
ostream&operator>ln.start.z>>ln.next.x>>ln.next.y>>ln.next.z;
返回;
}
int main()
{
点原点,输入;
线射线;
矢量侧;
//从文件中读取点
ifstream点文件(“凹面文件”);
if(pointfile.is_open())
{
点文件>>origin.x>>origin.y>>origin.z;

如果你还记得如何处理所有容器,你能通过投票按钮下的复选标记接受答案吗?这里很好地描述了你使用的向量。例如,非常感谢你的帮助!现在我找到了点与下一个点之间的距离。我仍在搜索总距离,即点与点之间距离的总和下一个。你的答案真的很有帮助。sumDistance-不是正确的答案?距离是每个段的总和,总和应该是所有段的总和。thanx 4接受;-)是的,距离不正确,因为我试图对最后一个进行求和,然后我找到了不同的a数。任何地方谢谢你的帮助!不确定是否还可以,以防让我知道-因为我不是英语母语,有时理解一些句子有点困难;-)如果你还记得如何处理所有容器,你可以通过投票按钮下的复选标记接受答案。你使用的向量在这里描述得很好,例如,非常感谢你的帮助!现在我发现点和下一个点之间的距离。我仍然在搜索点和下一个点之间距离的总和。你的答案真的很有帮助。sumDistance-不是正确的答案?距离是每个段的总和,总和应该是所有段的总和。thanx 4接受;-)是的,距离不正确,因为我试图最后一个句子的总结,然后我找到了不同的数字a。任何地方谢谢你的帮助!还不确定是否可以,以防让我知道-因为我不是英语母语,有时有些句子会有点难理解;-)汤姆非常感谢你的好答案!现在我的问题得到了回答。很抱歉,我的错误ce再次出现-可能在此处复制期间生成(?).顺便说一句,如果你不想失去声誉,试着多学一点编程,但对于简单的问题/答案/接受和15分;-)例如,我在每个问题上都失去了一些分数。汤姆非常感谢你的好答案!现在我的问题得到了回答。很抱歉,错误再次出现-可能是在复制过程中产生的在这里(?)。顺便说一句,如果你不想失去声誉,试着多学一点编程,但对于简单的问题/答案/接受和15分;-)例如,我在每个问题上都失去了一些分数。
#include <iostream>
#include <fstream>
#include <vector>
#include <conio.h>

using namespace std;

struct point
{
    float x;
    float y;
    float z;
};

ostream& operator<< (ostream& out, const point &p)
{
    out << "(" << p.x << "," << p.y << " ," << p.z << "," << ")";
    return out;
}

istream& operator>> (istream& in, point& point)
{
    in >> point.x >> point.y >> point.z;
    return in;
}

struct line
{
    point start;
    point next;
    double sqDistance()
    {
        float dx = start.x - next.x;
        float dy = start.y - next.y;
        float dz = start.z - next.z;
        double distance = 0.0;
        distance = sqrt(dx * dx + dy * dy + dz * dz);
        return distance;
    }
};

ostream& operator<< (ostream& out, const line &ln)
{
    out << "From " << ln.start << " to " << ln.next;
    return out;
}

istream& operator>> (istream& in, line ln)
{
    cout << "Enter x y z start then x y z  next: ";
    in >> ln.start.x >> ln.start.y >> ln.start.z >> ln.next.x >> ln.next.y >> ln.next.z;
    return in;
}

int main()
{
    point origin, input;
    line ray;
    vector<line> side;

    // READ POINTS FROM FILE
    ifstream pointfile("concave.txt");
    if (pointfile.is_open())
    {
        pointfile >> origin.x >> origin.y >> origin.z;
        cout << "origin: " << origin << endl;
        ray.start = origin;

        while (pointfile >> ray.next)
        {
            cout
                << " GOTO/ " << ray.next
                << " The distance from point to the next is : "
                << ray.sqDistance() << endl;

            side.push_back(ray);
            ray.start = ray.next; // set start to last end (?)
        }
    }
    else
        cout << "Unable to open file";
    pointfile.close();

    ofstream results("results.txt");
    vector<line>::iterator iter = side.begin();
    line closest = *iter;
    double distance, sumOfDistances = 0.0;
    cout << "Line coords" << endl << "distance | Sum of distances" << endl;
    while (iter != side.end()) {
        closest = *iter;
        distance = closest.sqDistance();
        sumOfDistances += distance;
        results << distance << endl;
        cout << closest << endl << distance << " | " << sumOfDistances << endl; // info output
        iter++;
    }
    results << sumOfDistances << " << Sum" << endl;
    results.close();
    cout << "Complete path distance: " << sumOfDistances << endl; // info output

    getch();

    return 0;
}