我對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();
}
}
一旦你有了在紙上找到交點的數學方法,實現它就不難了,1提示:做一類線。如果你想通過一個庫來實現,可以通過google和stackoverflow找到。 – stefaanv
你認爲2D還是3D? 2D中的線至少由x,y座標的兩個點描述,在x,y,z座標的3D兩個點中描述。你的載體如何與此相關? – 4pie0
@stefaanv謝謝你能爲我們提供這些圖書館的鏈接嗎? – user3101219