randomly generate polys on canvas Javascript

Viewed 26

I currently have two functions. The generateCars function works well as seen here:

//setting values for n and n2 
const N = 100;
const N2 = 100;
const cars = generateCars(N);
const traffic = generateTraffic(N2);

this is the generate cars function passing N as the value of cars to gen.

function generateCars(N){
    let cars=[];
    for(let i =1;i<=N;i++){
        cars.push(new Car(road.getLaneCenter(1),100,30,50,"AI"));
    }
    return cars;
}

However, the generateTraffic function is not. I am feeding generateTraffic two values within a range that are randomly selected each run. Even though these values are being read, the cars are being generated all on the same lane / y value.

//get randomInt function 
function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min) + min); // The maximum is exclusive and the minimum is inclusive
  }


function generateTraffic(N2){
    const traffic=[];
    const randomY = getRandomInt(-200, -100);
    const computerResponse = getRandomInt(0, 3);
    for(let i =1;i<=N2;i++){
        traffic.push(new Car(road.getLaneCenter(computerResponse),randomY,30,50,"DUMMY",2));
    }
    return traffic;
}

How would I go about generating the traffic objects each with random Lane values within a range of 0 -> 3 and Y values ranging from -200 -> -100?

0 Answers
Related