ورود

View Full Version : covex hull



shofer
سه شنبه 02 آبان 1391, 14:52 عصر
سلام

من دنبال کد اجرای convex hullبا الگوریتم 'graham scanهستم
به زبان C++

ممنون میشم کسی کمکم کنه
خیلی دنبالش گشتم و مقاله خوندم اما تو پیاده سازی کد مشکل دارم


با تشکر

shofer
سه شنبه 02 آبان 1391, 15:11 عصر
این کدیه که خودم دارم
چند خطا داره
ممنون میشم کمکم کنید





#include <random>
#include <algorithm>
#include <vector>
#include <stack>
#include <iostream>


using namespace std;

//**************************************

//************************************************** ***************
srand( static_cast<unsigned int>( time(NULL) ) );
for (size_t i = 0 ; i <N ; i++ ) {
int x = ( rand() % ( x_range.second - x_range.first + 1 ) ) + x_range.first;
int y = ( rand() % ( y_range.second - y_range.first + 1 ) ) + y_range.first;
raw_points.push_back( std::make_pair( x, y ) );
}


//************************************************** *********
int main(int argc, char* argv[])
{

std::ofstream gnuplot_file( "gnuplot.cmd" );
const int N = 20;
GrahamScan g( N, 0, 100, 0, 100 );




g.log_raw_points( std::cout );
g.plot_raw_points( gnuplot_file );
g.partition_points();
g.log_partitioned_points( std::cout );
g.plot_partitioned_points( gnuplot_file );
//
// Build the hull
//
g.build_hull( gnuplot_file );
g.log_hull( std::cout );
g.plot_hull( gnuplot_file, "complete" );
return 0;
}
//************************************************** ***************

//************************************************** ***************

void partition_points()
{


//
// Step one in partitioning the points is to sort the raw data
//
std::sort( raw_points.begin(), raw_points.end() );
//
// The the far left and far right points, remove them from the
// sorted sequence and store them in special members
//
left = raw_points.front();
raw_points.erase( raw_points.begin() );
right = raw_points.back();
raw_points.pop_back();
//
// Now put the remaining points in one of the two output sequences
//
for ( size_t i = 0 ; i <raw_points.size() ; i++ )
{
int dir = direction( left, right, raw_points[ i ] );
if ( dir <0 )
upper_partition_points.push_back( raw_points[ i ] );
else
lower_partition_points.push_back( raw_points[ i ] );
}
}

//************************************************** ***************


void build_hull( std::ofstream &f )
{
build_half_hull( f, lower_partition_points, lower_hull, 1 );
build_half_hull( f, upper_partition_points, upper_hull, -1 );
}

//************************************************** ***************


void build_half_hull( std::ostream &f,
std::vector<std::pair<int,int>> input,
std::vector<std::pair<int,int>> &output,
int factor )
{
// The hull will always start with the left point, and end with the right
// point. Initialize input and output accordingly
//
input.push_back( right );
output.push_back( left );
while ( input.size() != 0 ) {
//
// Repeatedly add the leftmost point to the null, then test to see
// if a convexity violation has occurred. If it has, fix things up
// by removing the next-to-last point in the output sequence until
// convexity is restored.
//
output.push_back( input.front() );
input.erase( input.begin() );
while ( output.size()>= 3 ) {
size_t end = output.size() - 1;
if ( factor * direction( output[ end - 2 ],
output[ end ],
output[ end - 1 ] ) <= 0 ) {
output.erase( output.begin() + end - 1 );
} else
break;
}
}
}