retour à project track
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define MAXOBJECTS 50
#include <time.h>
#include <vector>
using namespace std;
std::vector<std::vector <int> > genJardin (int combien, int largJardin, int hautJardin, int coteObjet)
{
int compte=0, i,x,y, x1, x2, y1, y2;
int largeur;
int hauteur;
int resteCote;
bool auquai = true;
std::vector<std::vector <int> > scenario(combien,std::vector<int>(2));
// int scenario [MAXOBJECTS][2];
cout << "les valeurs sont combien: " << combien
<<", largJardin: "<<largJardin
<< ", hautJardin: " << hautJardin
<<" et coteObjet : " << coteObjet << endl;
largeur = largJardin;
hauteur = hautJardin;
resteCote = coteObjet-1;
if (combien>MAXOBJECTS) {
cout<<combien<<" objets demand�s, seulement "<<combien <<" (au plus) seront plac�s"<<endl;
combien=MAXOBJECTS;
}
if ((coteObjet>largeur)|(coteObjet>hauteur)){cout<<"Objets trop grand pour ce terrain!"
<< endl; combien=0;}
if ((coteObjet*coteObjet*combien)>(hauteur*largeur*2)){
combien = (hauteur*largeur*2)/(coteObjet*coteObjet);
cout<<"Potentiellement trop d'objets pour placer facilement, objectif r�duit � "
<< combien << endl;
}
/* initialize random seed: */
srand ( time(NULL) );
/* generate secret number: */
while (compte<combien) {
x = rand() % largeur + 1;
y = rand() % hauteur + 1;
auquai=true;
/*
* d'abord, verifier que l'objet reste dans le terrain
*/
if ((x+resteCote>largeur)|(y+resteCote>hauteur))auquai=false;
/*
* voir si point est dans un carr� d�j� r�serv�
* 1 cas : (x,y) est dans le carr�
* 2 cas : (x+resteCote, y) est dans le carr�
* 3 cas : (x,y+resteCote) est dans le carr�
* dernier cas : (x+resteCote, y+resteCote) -- le coin oppos�-- est dans le carr�
*/
for (i=0;auquai&(i<compte);i++){
x1 = scenario[i][0];
y1 = scenario[i][1];
x2 = x1 + resteCote;
y2 = y1 + resteCote;
if (auquai & (x>=x1) & (x <=x2)
& (y>=y1) & (y<=y2)) auquai=false;
if (auquai & (x+resteCote>=x1) & (x+resteCote <=x2)
& (y>=y1) &( y <= y2)) auquai=false;
if (auquai & (x>=x1) & (x <=x2)
& (y+resteCote >= y1) &(y+resteCote <= y2)) auquai=false;
if (auquai & (x+resteCote>=x1) & (x+resteCote <=x2)
& (y+resteCote >= y1) & (y+resteCote <= y2)) auquai=false;
} // next i
if (auquai) {
scenario[compte][0]=x;
scenario[compte][1]=y;
compte++;
cout<<"objet "<<compte<<" = ("<< x<<", "<<y<<")\n"<<endl;
}
} // fin while
return scenario;
}
int
main ()
{
int combien, largJardin, hautJardin, coteObjet;
//int coins [MAXOBJECTS][2];
std::vector<std::vector <int> > coins;
int i;
cout<<"combien d'objets sont a placer?"<<endl;
cin >> combien;
cout <<"\n combien mesure le cote d'un objet?" <<endl;
cin >> coteObjet;
cout << "\n Quelle est la largeur du terrain?"<<endl;
cin >> largJardin;
cout <<"\n Et enfin, quelle est la hauteur du terrain?"<<endl;
cin >> hautJardin;
coins = genJardin(combien, largJardin, hautJardin, coteObjet);
for (i=0;i<combien;i++)
{
cout << "objet "<< i <<" = ("<< coins[i][0]<<", "<< coins[i][1] <<")\n"<<endl;
}
return 0;
}