First of all, there are loooooot of ways to do procedural generation and none of them is easy at all, i will make kind of approach to how you could make it work, is up to you take it, modify it or discard it.
Will pseudo-code in JS as it's easier to understand.
1º define an entry point, as you want to build a medieval city we gonna start with a square, so lets say your town will have 300 squared units and the square will be in the middle of it (represented with an X).
300
________________
| |
| |
| | 300
| X |
| |
| |
|_______________|
const square = [ 150, 150 ];
2º now we will the avenues, there will be a random number of them, they will be straight and will start from the middle square or from other avenues
let avenues = [] // will contain start and end [[sx,sy],[ex,ey]]
const n_avenues = RANDOM(4, 8); // number of avenues
const n_av_from_square = RANDOM(0, avenues); // starting in the square
for av in av_from_square
avenues.push(square, [RANDOM(0, 200) + 100, RANDOM(0, 200) + 100])
// we want avenues to have, at least 100 units length, thats why we randomize just te last 200 units of the whole town size
This should give you a square and couple main streets
300
________________
| \\ |
| \\ |
| \\ | 300
| X===== |
| |
| |
|_______________|
Now we have to set the avenues that doesn't start in the main square, they will intersect the other avenues
for av in (n_avenues - av_from_square){
const av_to_intersect = avenues[RANDOM(0,avenues.length)];
//check av_to... and get a perpendicular vector (explained bellow)
av[0] = [ av_to_intersect[0][1], - av_to_intersect[0][0] ];
av[1] = [ av_to_intersect[1][1], - av_to_intersect[1][0] ];
}
To get perpendicular vectors you have to swap the x,y cords and negate the new y:
swiped == x: noswiped.y, y: -1 * (noswiped.x)
Right now you should have something similar to this, doesnt it look like a town? :P
300
________________
| \\ // |
| \\// || |
| \\ || | 300
| //\X===== |
| // || |
| || |
|_______________|
3º now you only have to interconnect the avenues with short streets, also, you could spawn random squares all along the town and make the same as above for all of them, or just spawn small streets from some secundary squares, it's up to you.
Remember, the shortest your streets are, the chaotic the town looks like.