help with this program

Reply

Join Date: Nov 2008
Posts: 1
Reputation: DarkSam is an unknown quantity at this point 
Solved Threads: 0
DarkSam DarkSam is offline Offline
Newbie Poster

help with this program

 
0
  #1
Nov 25th, 2008
I need help with this problem


Generating Random Cities

Write a function called generate_cities(min_x, max_x, min_y, max_y, n, distance) that generates a list of cities with random coordinates. The parameters represent the following:

* min_x, max_ x: the minimum and maximum values for the x coordinate of each city
* min_y max_ y: the minimum and maximum values for the y coordinate of each city
* n: the number of cities to be generated (that is the number of [x, y] lists)
* distance: the minimum distance allowed between any two cities

Here is a brief description of how you can generate your cities while ensuring that they are not too close to each other

This is the algorithm that I have to follow
repeat n times
generate random coordinates for new_city
too_close = True
while too_close == True
too_close = False
for each city in the city list so far
if the new_city < distance from city
too_close = True
generate new random city point
break
add new_city to the results list

This is as far as I got:

def generate_cities(min_x, max_x, min_y, max_y, n, distance):
for steps in range(N):
random_coordinates_x = random.randint(min_x, max_x)
random_coordinates_y = random.randint(min_y, max_y)
new_city = [random_coordinates_x, random_coordinates_y]
print new_city
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: help with this program

 
0
  #2
Nov 25th, 2008
where's the code to support the rest of the algorithm?

It looks like it might be useful to write a function to calculate the distance between two cities (or two coordinates).
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC