首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在c++中,如何计算工具路径长度?

在c++中,如何计算工具路径长度?
EN

Stack Overflow用户
提问于 2019-05-20 16:16:16
回答 2查看 348关注 0票数 1

我正在处理生成的刀具路径,其中包含多个点在三维,我是用数控机床来生成它们。我想要计算的事情之一是工具路径长度,它定义了路径的总长度。所以我试了一下:

代码语言:javascript
运行
复制
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 

要点是这样的。

代码语言:javascript
运行
复制
// 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;
}

-I期望点到下一个点之间的距离。

这一行的-the总长度。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-20 17:56:30

这样的情况如何?假设您想要点之间的距离,而不是从开始(第93行加上注释):

代码语言:javascript
运行
复制
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
#include<math.h>
#include <conio.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);
            ray.start = ray.next; // set start to last end (?)
        }
    }
    else
        cout << "Unable to open file";
    pointfile.close();

    vector<line>::iterator iter = side.begin();
    line temp, closest = *iter;
    float minimumDistance = closest.sqDistance(), distance, sumDistance = 0.0;
    cout << "Line coords" << endl << "distance, Sum of distances, minimum" << endl;
    while(iter != side.end()) {
        closest = *iter;
        distance = closest.sqDistance();
        sumDistance += distance;
        if(minimumDistance > distance) minimumDistance = distance;
        cout << closest << endl
              << distance << " | " << sumDistance << " | " << minimumDistance << endl;
        sumDistance += distance;
        iter++;
    }

    getch();

    return 0;
}

对不起,bug - sum行在cout之后出现了两行错误,还注意到一些精度警告--混合双/浮点数,因此切换到到处都是double,现在主循环如下所示:

代码语言:javascript
运行
复制
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;
    cout << closest << endl << distance << " | " << sumOfDistances << endl; // step info output
    iter++;
}
票数 0
EN

Stack Overflow用户

发布于 2019-05-23 06:18:35

在这里完成,一个更短的版本,包括简单的results.txt文件输出。您可以删除2行注释信息输出:

代码语言:javascript
运行
复制
#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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56224679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档