2014-03-03 131 views
0

我对C++编程语言非常新颖,我只需要知道如何声明一组给定它们的开始点和结束点的线段?在C++中是否有类似的东西? 我有这样的代码,它从文本文件中读取线段的起点和终点,并将输入分为4个矢量:X_start,Y_start,X_end,Y_end。 我需要知道如何使用这些向量来定义线段?任何帮助,将不胜感激。在此先感谢我如何找到2线段之间的交点坐标C++

#include <iostream> 
#include <algorithm> // for std::copy#include <iostream> 
#include <iterator> 
#include <fstream> 
#include <math.h> 
#include <vector> 
#include <algorithm> // for std::copy 
using namespace std; 

int main() 
{ 
std::ifstream is("D:\\Task1.txt"); 
std::istream_iterator<double> start(is), end; 
std::vector<double> numbers(start, end); 
std::vector<double> X_start(start, end); 
std::vector<double> Y_start(start, end); 
std::vector<double> X_end(start, end); 
std::vector<double> Y_end(start, end); 
std::vector<double>::iterator i; 
std::vector<double>::iterator j; 
float left, top, right, bottom; // Bounding Box For Line Segments 
left = 12; 
top = 12; 
right = 0; 
bottom = 0; 

std::cout << "Read " << numbers.size() << " numbers" << std::endl; 
std::copy(numbers.begin(), numbers.end(), 
     std::ostream_iterator<double>(std::cout, " ")); 
std::cout << std::endl; 

for (vector<double>::iterator i = numbers.begin(); 
         i != numbers.end(); 
         ++i) 
{ 
for(int j = 0; j < numbers.size(); j = j+4) 
{ 
std::cout << "elemnts of X_start " << numbers[j] << " " <<std::endl; 
X_start.push_back(numbers[j]); 

} 
for(int k = 1; k < numbers.size(); k = k+4) 
{ 
std::cout << "elemnts of Y_start " << numbers[k] << " " <<std::endl; 
Y_start.push_back(numbers[k]); 
} 
for(int l = 2; l < numbers.size(); l = l+4) 
{ 
std::cout << "elemnts of X_end " << numbers[l] << " " <<std::endl; 
X_end.push_back(numbers[l]); 
} 
for(int m = 3; m < numbers.size(); m = m+4) 
{ 
std::cout << "elemnts of Y_end " << numbers[m] << " " <<std::endl; 
Y_end.push_back(numbers[m]); 
} 
getchar(); 
} 
} 
+0

一旦你有了在纸上找到交点的数学方法,实现它就不难了,1提示:做一类线。如果你想通过一个库来实现,可以通过google和stackoverflow找到。 – stefaanv

+0

你认为2D还是3D? 2D中的线至少由x,y坐标的两个点描述,在x,y,z坐标的3D两个点中描述。你的载体如何与此相关? – 4pie0

+0

@stefaanv谢谢你能为我们提供这些图书馆的链接吗? – user3101219

回答

0

我使用以下函数。只需修改它以符合您的要求。

class Point 
{ 
    public: 
    float x,y; 
}; 


class LineSegment 
{ 
    public: 
    Point top; 
    Point bottom; 
}; 

Point* getIntersectionPoint(LineSegment line1,LineSegment line2) 
{ 
    float s1_x, s1_y, s2_x, s2_y; 
    float p1_x = line1.bottom.x,p1_y = line1.bottom.y; 
    float p0_x = line1.top.x,p0_y = line1.top.y; 
    float p3_x = line2.bottom.x,p3_y = line2.bottom.y; 
    float p2_x = line2.top.x,p2_y = line2.top.y; 
    s1_x = p1_x - p0_x;  s1_y = p1_y - p0_y; 
    s2_x = p3_x - p2_x;  s2_y = p3_y - p2_y; 

    float s, t; 
    s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y))/(-s2_x * s1_y + s1_x * s2_y); 
    t = (s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x))/(-s2_x * s1_y + s1_x * s2_y); 

    if (s >= 0 && s <= 1 && t >= 0 && t <= 1) 
    { 
     Point *intsPoint = (Point *)malloc(sizeof(Point)); 
     intsPoint->x = p0_x + (t * s1_x); 
     intsPoint->y = p0_y + (t * s1_y); 
     return intsPoint; 
    } 

    return NULL; 
} 
+0

为什么intsPoint被分配在堆上?也请删除它,以避免内存泄漏 – 4pie0

+0

@piotruś大概允许函数在两个线段* not * intersect事件中返回null。因为答案并没有提供对该方法的调用,所以您提出的删除应该去哪里!? –

+0

你应该返回值 – 4pie0

0

“我很新的C++编程语言”

如何新的C++?你知道你首先想要做什么的数学吗?

+0

是的其实我是这样做的,但我不知道如何在C++中定义一组线段等等, 数学不是我的问题 – user3101219

+0

代码是从头开始做的吗?或从某处复制(来源?) –

+0

没有没有复制它,我正努力学习我自己的C++,但我只是一个初学者 – user3101219

相关问题