Click here to Skip to main content
15,891,513 members
Home / Discussions / Algorithms
   

Algorithms

 
GeneralRe: Multiplayer game rating Pin
LongRange.Shooter1-Feb-08 10:33
LongRange.Shooter1-Feb-08 10:33 
GeneralFinding Closest Points in 2 dimensions Pin
MikeMarq12-Jan-08 18:24
MikeMarq12-Jan-08 18:24 
GeneralRe: Finding Closest Points in 2 dimensions Pin
cp987612-Jan-08 23:04
cp987612-Jan-08 23:04 
GeneralRe: Finding Closest Points in 2 dimensions Pin
Member 419459314-Mar-08 6:36
Member 419459314-Mar-08 6:36 
GeneralRe: Finding Closest Points in 2 dimensions Pin
cp98768-May-08 16:14
cp98768-May-08 16:14 
GeneralRe: Finding Closest Points in 2 dimensions Pin
CPallini14-Jan-08 2:24
mveCPallini14-Jan-08 2:24 
GeneralRe: Finding Closest Points in 2 dimensions Pin
Alan Balkany14-Jan-08 3:47
Alan Balkany14-Jan-08 3:47 
GeneralRe: Finding Closest Points in 2 dimensions [modified] Pin
Stephen Hewitt14-Jan-08 11:18
Stephen Hewitt14-Jan-08 11:18 
Simply use a point where x is the average of all the x coordinates and y is the average of all the y coordinates. For example, in standard C++:
// VanillaConsole.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
#include <iostream>
#include <ostream>
#include <iterator>	// For 'ostream_iterator'.
#include <algorithm>	// For 'copy'.
#include <numeric>		// For 'accumulate'.
using namespace std;
 
// Structure to represent a point.
struct point
{
	point()
		: x(0), y(0) {}
	point(double x, double y)
		: x(x), y(y) {}
 
	double x;
	double y;
};
 
// Add two points together.
point operator+(const point &lhs, const point &rhs)
{
	return point(lhs.x+rhs.x, lhs.y+rhs.y);
}
 
// Divide a point by a number.
point operator/(const point &lhs, double rhs)
{
	return point(lhs.x/rhs, lhs.y/rhs);
}
 
// Output a point.
ostream& operator<<(ostream &os, const point &pt)
{
	os << "(" << pt.x << ", " << pt.y << ")";
	return os;
}
 
int main(int argc, char* argv[])
{
	// Our collection of points.
	const point pts[] =
	{
		point(1.0, 2.0),
		point(2.0, 1.0),
		point(3.0, 3.0),
		point(4.0, 4.0),
		point(-1.0, 3.0)
	};
	const point *pEnd = pts + sizeof(pts)/sizeof(pts[0]);
 
	// Output the points to the console.
	ostream_iterator<point> osi(cout, "\n");
	copy(pts, pEnd, osi);
	cout << endl;
 
	// Now calculate the closest point.
	point pt;
	pt = accumulate(pts, pEnd, pt);
	pt = pt/(sizeof(pts)/sizeof(pts[0]));
 
	// Output the result.
	cout << "Closest point is " << pt << endl;
 
	return 0;
}


Steve

modified on Monday, January 14, 2008 6:25:06 PM

GeneralRe: Finding Closest Points in 2 dimensions Pin
Alan Balkany16-Jan-08 3:41
Alan Balkany16-Jan-08 3:41 
GeneralRe: Finding Closest Points in 2 dimensions Pin
CPallini17-Jan-08 2:10
mveCPallini17-Jan-08 2:10 
GeneralRe: Finding Closest Points in 2 dimensions Pin
MikeMarq17-Jan-08 12:13
MikeMarq17-Jan-08 12:13 
GeneralRe: Finding Closest Points in 2 dimensions Pin
cp987617-Jan-08 15:18
cp987617-Jan-08 15:18 
GeneralRe: Finding Closest Points in 2 dimensions Pin
brien_rtb17-Jan-08 18:00
brien_rtb17-Jan-08 18:00 
GeneralRe: Finding Closest Points in 2 dimensions [modified] Pin
Skippums21-Jan-08 8:58
Skippums21-Jan-08 8:58 
GeneralMulti-dimensional surface representation and interpolation in C++ Pin
sjcomp12-Jan-08 11:47
sjcomp12-Jan-08 11:47 
GeneralRe: Multi-dimensional surface representation and interpolation in C++ Pin
CKnig22-Jan-08 3:09
CKnig22-Jan-08 3:09 
GeneralRe: Multi-dimensional surface representation and interpolation in C++ Pin
sjcomp22-Jan-08 6:48
sjcomp22-Jan-08 6:48 
GeneralOne Big doubt in a small algo... Pin
selvabsc11-Dec-07 1:19
selvabsc11-Dec-07 1:19 
GeneralRe: One Big doubt in a small algo... Pin
Luc Pattyn11-Dec-07 4:19
sitebuilderLuc Pattyn11-Dec-07 4:19 
Questionhelpppp Pin
FREAK880230-Nov-07 7:25
FREAK880230-Nov-07 7:25 
AnswerRe: helpppp Pin
Dan Neely30-Nov-07 7:57
Dan Neely30-Nov-07 7:57 
GeneralRe: helpppp Pin
FREAK880230-Nov-07 8:20
FREAK880230-Nov-07 8:20 
GeneralRe: helpppp Pin
Dan Neely30-Nov-07 8:42
Dan Neely30-Nov-07 8:42 
GeneralRe: helpppp Pin
Paul Conrad1-Dec-07 9:27
professionalPaul Conrad1-Dec-07 9:27 
GeneralRe: helpppp Pin
Maximilien30-Nov-07 8:50
Maximilien30-Nov-07 8:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.