Browse Source

version 2

master
Victor Giers 5 years ago
parent
commit
b8d2a410e0
3 changed files with 111 additions and 47 deletions
  1. 90
    0
      Houses.pde
  2. 5
    0
      UI.pde
  3. 16
    47
      citizen.pde

+ 90
- 0
Houses.pde View File

@@ -0,0 +1,90 @@
import java.util.ArrayDeque;

PImage img_houses;
PImage img_spawnPoints;
int i_biggestHouse;

ArrayList<SpawnArea> spawnAreas = new ArrayList<SpawnArea>();

void initHouses() {
//get houses from image
img_houses = loadImage(dataPath("map/houses_with_borders.png"));


img_houses.loadPixels();
for (int x = 0; x < img_houses.width; x++) {
for (int y = 0; y < img_houses.height; y++) {
if (int(red(img_houses.pixels[y*img_houses.width+x])) == 187) { //red must be 187 for a pixel to be part of a house
spawnAreas.add(new SpawnArea(x, y));
img_houses.updatePixels();
img_houses.loadPixels();
}
}
}
img_houses.updatePixels();
for(SpawnArea spawn : spawnAreas){
spawn.i_size = int(map(spawn.i_size,0,i_biggestHouse,0,100));
}
}

class SpawnArea {
PVector v2_center;
int i_size;
void display(){
text(i_size,v2_center.x,v2_center.y);
}
ArrayDeque<PVector> v2d_selected = new ArrayDeque<PVector>();
ArrayList<PVector> v2d_points = new ArrayList<PVector>();
boolean isToSelect(int px, int py, int[] pxl, int pw, int ph, int orgColor) {
if (px < 0 || px >= pw || py < 0 || py >= ph)
return false;
return pxl[px + py * pw] == orgColor;
}
SpawnArea(int startX, int startY) {
int [] pxl = img_houses.pixels;
int pw = img_houses.width;
int ph = img_houses.height;
int orgColor = pxl[startX + startY * pw];
PVector v2_p = new PVector(startX, startY);
color randCol = color(int(random(0, 255)), int(random(0, 255)), int(random(0, 255)));
v2d_selected.add(v2_p);
int west, east;
while (!v2d_selected.isEmpty () ) { //&& q.size() < 500) {
v2_p = v2d_selected.removeFirst();
if (isToSelect(int(v2_p.x), int(v2_p.y), pxl, pw, ph, orgColor)) {
west = east = int(v2_p.x);
while (isToSelect(--west, int(v2_p.y), pxl, pw, ph, orgColor));
while (isToSelect(++east, int(v2_p.y), pxl, pw, ph, orgColor));
for (int x = west + 1; x < east; x++) {
v2d_points.add(new PVector(x, int(v2_p.y)));
pxl[x + int(v2_p.y) * pw] = color(188, 188, 188); //important for not selecting the same area more than once
if (isToSelect(x, int(v2_p.y) - 1, pxl, pw, ph, orgColor))
v2d_selected.add(new PVector(x, int(v2_p.y) - 1));
if (isToSelect(x, int(v2_p.y) + 1, pxl, pw, ph, orgColor))
v2d_selected.add(new PVector(x, v2_p.y + 1));
}
}
}
i_size = v2d_points.size();
PVector avgPoint = new PVector(0,0);
for (PVector point : v2d_points) {
avgPoint.add(point);
}
avgPoint.x = avgPoint.x/i_size;
avgPoint.y = avgPoint.y/i_size;
v2_center = avgPoint;
if(i_size > i_biggestHouse) i_biggestHouse = i_size;
}
}

+ 5
- 0
UI.pde View File

@@ -0,0 +1,5 @@
void UI() {

textSize(30);
text(int(frameRate), 30, 30);
}

+ 16
- 47
citizen.pde View File

@@ -1,15 +1,10 @@
/*asd*/
import gab.opencv.*;


OpenCV opencv;

PImage img_streets;
PImage img_houses;
PGraphics pg_map;

boolean b_isRaining;


class Citizen{
int i_xSpawn, i_ySpawn, i_xPos, i_yPos;
String s_clan;
@@ -41,60 +36,34 @@ class Citizen{

Citizen mensch;

//distributionArea =

void setup(){
size(777,777);
textSize(30);
void setup() {
size(400, 300);
textAlign(CENTER);
fill(0);
opencv = new OpenCV( this );
img_streets = loadImage(dataPath("map/streets.png"));
img_houses = loadImage(dataPath("map/houses_with_borders.png"));
surface.setResizable(true);
surface.setSize(img_streets.width,img_streets.height);
initHouses();
img_streets = loadImage(dataPath("map/streets.png"));
blendMode(MULTIPLY);
surface.setSize(img_houses.width, img_houses.height);
generate_Spawns();

mensch = new Citizen();
mensch.spawn();
}

SpawnArea[] spawnAreas;

class SpawnArea{
//simplified hull?
SpawnArea(){
}
}

void generate_Spawns(){
img_houses.loadPixels();
//needs proper blob-detection
for(int y = 0; y < img_houses.height; y++){
for(int x = 0; x < img_houses.width; x++){
float r = red(img_houses.pixels[(y*img_houses.width)+x]);
if(r == 187) point(x, y);
}
void draw() {
background(255);
image(img_houses, 0, 0, img_houses.width, img_houses.height);
image(img_streets, 0, 0, img_houses.width, img_houses.height);
textSize(7);
for (SpawnArea spawn : spawnAreas) {
spawn.display();
}
img_houses.updatePixels();
}

void draw(){
background(255);
image(img_streets,0,0,img_streets.width, img_streets.height);
image(img_houses,0,0,img_houses.width, img_houses.height);

mensch.update();
text(int(frameRate),0,30);

UI();
}

Loading…
Cancel
Save