コード例 #1
1
void QgsPointSample::addSamplePoints( QgsFeature& inputFeature, QgsVectorFileWriter& writer, int nPoints, double minDistance )
{
  if ( !inputFeature.constGeometry() )
    return;

  const QgsGeometry* geom = inputFeature.constGeometry();
  QgsRectangle geomRect = geom->boundingBox();
  if ( geomRect.isEmpty() )
  {
    return;
  }

  QgsSpatialIndex sIndex; //to check minimum distance
  QMap< QgsFeatureId, QgsPoint > pointMapForFeature;

  int nIterations = 0;
  int maxIterations = nPoints * 200;
  int points = 0;

  double randX = 0;
  double randY = 0;

  while ( nIterations < maxIterations && points < nPoints )
  {
    randX = (( double )mt_rand() / MD_RAND_MAX ) * geomRect.width() + geomRect.xMinimum();
    randY = (( double )mt_rand() / MD_RAND_MAX ) * geomRect.height() + geomRect.yMinimum();
    QgsPoint randPoint( randX, randY );
    QgsGeometry* ptGeom = QgsGeometry::fromPoint( randPoint );
    if ( ptGeom->within( geom ) && checkMinDistance( randPoint, sIndex, minDistance, pointMapForFeature ) )
    {
      //add feature to writer
      QgsFeature f( mNCreatedPoints );
      f.setAttribute( "id", mNCreatedPoints + 1 );
      f.setAttribute( "station_id", points + 1 );
      f.setAttribute( "stratum_id", inputFeature.id() );
      f.setGeometry( ptGeom );
      writer.addFeature( f );
      sIndex.insertFeature( f );
      pointMapForFeature.insert( mNCreatedPoints, randPoint );
      ++points;
      ++mNCreatedPoints;
    }
    else
    {
      delete ptGeom;
    }
    ++nIterations;
  }
}
コード例 #2
0
ファイル: SnakeGame.cpp プロジェクト: Madsy/GameAutomata
void snakeInitSnakes(SnakeGameInfo& state, int playerCount)
{
    /* IsCellClear checks whether the rpart position collides with
     other snakes, so init state.snakes[i].parts[j] to be outside the map bounds to avoid this. */
  Point point_outside_map(-1, -1);
  SnakeInfo tmpSnake;
  std::vector<Point> tmpParts;

  state.playerCount = playerCount;
  state.currentPlayer = 0;
  /* TODO: Fix so that we can have longer snakes at the beginning of the level.
     Currently we start with snakes of 1 in length. We rather want three-celled snakes. */
  tmpParts.push_back(point_outside_map);
  //tmpParts.push(point_outside_map);
  //tmpParts.push(point_outside_map);
  tmpSnake.bodyParts = tmpParts;
  tmpSnake.alive = true;
  /* Whenever growCount > 0, we move the snake body without updating the tail,
     and decrement growCount accordingly. */
  tmpSnake.growCount = 0;

  state.snakes.resize(playerCount);
  /* Initialize all snakes to be outside the map */
  for(int eachSnake = 0; eachSnake < playerCount; ++eachSnake)
    state.snakes[eachSnake] = tmpSnake;

  for(int eachSnake = 0; eachSnake < playerCount; ++eachSnake){
    Point rpart;
    do {
      rpart = randPoint(0, state.levelWidth - 1, 0, state.levelHeight - 1);
    } while(!snakeIsCellClear(rpart.x, rpart.y, -1, state));
    state.snakes[eachSnake].bodyParts[0] = rpart;
  }
}
コード例 #3
0
ファイル: graphics.cpp プロジェクト: ej2xu/cs106b
int main () {
	Point v1, v2, v3, currentPoint;
	InitGraphics();
	createTriangle(v1, v2, v3);
	currentPoint = randPoint(v1, v2, v3);
	iterate(v1, v2, v3, currentPoint);
	return 0;
}
コード例 #4
0
ファイル: SnakeGame.cpp プロジェクト: Madsy/GameAutomata
void snakeUpdateFood(SnakeGameInfo& state)
{
  Point point_outside_map(-1, -1);
  Point rfood;
  state.foodPosition = point_outside_map;
  do {
    rfood = randPoint(0, state.levelWidth, 0, state.levelHeight);
  } while(!snakeIsCellClear(rfood.x, rfood.y, -1, state));
  state.foodPosition = rfood;
}
コード例 #5
0
ファイル: graphics.cpp プロジェクト: ej2xu/cs106b
void iterate(Point v1, Point v2, Point v3, Point currentPoint) {
	if (MouseButtonIsDown()) ExitGraphics();
	MovePen(currentPoint.X, currentPoint.Y);
	StartFilledRegion(1);
    DrawArc(CIRCLE_SIZE, 0, 360);
    EndFilledRegion();
	Point nextRand = randPoint(v1, v2, v3);
	currentPoint.X = 0.5 * (currentPoint.X + nextRand.X);
	currentPoint.Y = 0.5 * (currentPoint.Y + nextRand.Y);
	UpdateDisplay();
	iterate(v1, v2, v3, currentPoint);
}
コード例 #6
0
ファイル: SnakeGame.cpp プロジェクト: Madsy/GameAutomata
void snakeInitFood(SnakeGameInfo& state)
{
  Point point_outside_map(-1, -1);
  Point rfood;
  /* SnakeIsCellClear checks if the rfood position collides with
     state.foodPosition, so init state.foodPosition outside the map bounds to avoid that. */
  state.foodPosition = point_outside_map;
  do {
    rfood = randPoint(0, state.levelWidth, 0, state.levelHeight);
  } while(!snakeIsCellClear(rfood.x, rfood.y, -1, state));
  state.foodPosition = rfood;
}
コード例 #7
0
ファイル: board.cpp プロジェクト: ShallWeGo/Board2048
bool Board2048::randKey(int Num){
try
	{
	for(int i=0;i<Num;i++){
		int value = rand()%2;
		Point pt = randPoint();
		if(pt.x == -1 && pt.y == -1)
			throw exception();
		board[pt.x][pt.y] = (value+1)*2;
		/*  Increase the KeyNum */
		keyNum++;
	}
	}
catch(exception e){
	cout<<"No extra Position!!"<<endl;
}
}
コード例 #8
0
void PoissonDiskSampling::objectsPosition(WorldMap* map) {
	float minDistance = DISTANCE;

	std::vector <sf::Vector2f*> activePoints;
	//contenner in witch we are putting ceils with points.
	Grid grid;


	float cellSize = minDistance / SQRT2;
	grid.resize((mapWidth / cellSize), std::vector<bool>((mapHeight / cellSize), false));

	sf::Vector2f* firstPoint = randPoint();
	sf::Vector2i baseGrid;
	baseGrid = getGrid(firstPoint, cellSize);
	activePoints.push_back(firstPoint);

	positions[baseGrid.x][baseGrid.y].push_back(firstPoint);
	grid[baseGrid.x][baseGrid.y] = true;
	while (!activePoints.empty()) {

		sf::Vector2f* basePoint = (*activePoints.begin());
		baseGrid = getGrid(basePoint, cellSize);
		int z = KMAX;
		for (int i = 0; i < z; ++i) {

			sf::Vector2f* neighbour = randNeighbour(basePoint, minDistance);
			sf::Vector2i neighbourGrid = getGrid(neighbour, cellSize);

			if (neighbour->x>0 && neighbour->y > 0 && neighbour->x < mapWidth && neighbour->y < mapHeight) {
				if (map->getMap(neighbour->x, neighbour->y, 0)>127) {
					// if ok add to active, grid and objectsPosition
					if (checkNeighbour(neighbour, minDistance, grid, positions)) {
						activePoints.push_back(neighbour);
						grid[neighbourGrid.x][neighbourGrid.y] = true;
						positions[neighbourGrid.x][neighbourGrid.y].push_back(neighbour);
						ilosc++;
					}
				}
			}
		}

		// delete previous random from active
		activePoints.erase(activePoints.begin());
	}
}