Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hexagons #13

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
40 changes: 36 additions & 4 deletions src/main/java/com/opennars/applications/crossing/Crossing.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
import processing.event.MouseEvent;

public class Crossing extends PApplet {
public static double hexagonWidth = 10.0;
public static double hexagonHeight = 10.0;

static HexagonMapping hexagonMapping = new HexagonMapping(hexagonWidth, hexagonHeight);

Nar nar;
int entityID = 1;

Expand Down Expand Up @@ -111,12 +116,15 @@ public void draw() {
nar.addInput(questions);
}
}
for (int i = 0; i < 1000; i += Util.discretization) {
stroke(128);
line(0, i, 1000, i);
line(i, 0, i, 1000);

for (int y=50;y<100;y++) {
for (int x=25;x<75;x++) {
final double[] pos = hexagonMapping.calcPositionOfHexagon(x, y);
drawHexagon(pos[0], pos[1]);
}
}


for (Entity e : entities) {
e.draw(this, streets, trafficLights, entities, null, 0);
}
Expand Down Expand Up @@ -157,6 +165,17 @@ public void draw() {
System.out.println("Concepts: " + nar.memory.concepts.size());
}

private void drawHexagon(final double x, final double y) {
stroke(128);

for (int i=0; i < relatives.length; i++) {
final double[] aRel = relatives[i];
final double[] bRel = relatives[(i+1) % relatives.length];

line((float)(x + aRel[0]), (float)(y + aRel[1]), (float)(x + bRel[0]), (float)(y + bRel[1]));
}
}

public void removeOutdatedPredictions(List<Prediction> predictions) {
List<Prediction> toDelete = new ArrayList<Prediction>();
for(Prediction pred : predictions) {
Expand Down Expand Up @@ -213,4 +232,17 @@ public static void main(String[] args) {
new IncidentSimulator().show();
PApplet.runSketch(args2, mp);
}


final double[][] relatives = new double[][]{
{hexagonMapping.width * -0.5, hexagonMapping.height * -0.25},
{hexagonMapping.width * -0.5, hexagonMapping.height * 0.25},

{0.0, hexagonMapping.height * 0.5},

{hexagonMapping.width * 0.5, hexagonMapping.height * 0.25},
{hexagonMapping.width * 0.5, hexagonMapping.height * -0.25},

{0.0, hexagonMapping.height * -0.5},
};
}
110 changes: 110 additions & 0 deletions src/main/java/com/opennars/applications/crossing/HexagonMapping.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* The MIT License
*
* Copyright 2018 The OpenNARS authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.opennars.applications.crossing;

public class HexagonMapping {
public double width; // width of hexagon
public double height; // height of hexagon

public HexagonMapping(final double width, final double height) {
this.width = width;
this.height = height;
}

public double[] calcPositionOfHexagon(final int x, final int y) {
final double offsetX = (y % 2) == 0 ? 0.0 : -0.5*width;

double resultX = (x + 1) * width + offsetX;
double resultY = 0.5*height + 0.75*height*y;
return new double[]{resultX, resultY};
}

public Vec2Int map(final double x, final double y) {
final int ix = (int)(x / (width*2.0));
final int iy = (int)(y / (height*1.5));

final double relX = x - ix * (width*2.0);
final double relY = y - iy * (height*1.5);

final Vec2Int relativeHexagonIndices = mapGroupToRelCell(relX, relY);
return new Vec2Int(relativeHexagonIndices.x + ix*2, relativeHexagonIndices.y + iy*2);
}

private Vec2Int mapGroupToRelCell(final double x, final double y) {
// see https://www.redblobgames.com/grids/hexagons/ for illustration

double[][] positions = new double[][]{
{0.5 * width, -0.25 * height},
{1.5 * width, -0.25 * height},

{0.0, 0.5 * height},
{width, 0.5 * height},
{width + width, 0.5 * height},

{0.5 * width, 1.25 * height},
{1.5 * width, 1.25 * height},
};

double[] distances = new double[positions.length];

for(int i=0;i<positions.length;i++) {
distances[i] = Util.distance(x, y, positions[i][0], positions[i][0]);
}

int minDistanceIdx = 0;
double minDistance = distances[0];
for(int i=0;i<distances.length;i++) {
if (distances[i] < minDistance) {
minDistanceIdx = i;
minDistance = distances[i];
}
}


// fetch coordinate by index
if (minDistanceIdx == 0) {
return new Vec2Int(0, -1);
}
else if (minDistanceIdx == 1) {
return new Vec2Int(1, -1);
}

else if (minDistanceIdx == 2) {
return new Vec2Int(-1, 0);
}
else if (minDistanceIdx == 3) {
return new Vec2Int(0, 0);
}
else if (minDistanceIdx == 4) {
return new Vec2Int(1, 0);
}

else if (minDistanceIdx == 5) {
return new Vec2Int(0, 1);
}
else {
return new Vec2Int(1, 1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,13 @@ public Prediction predictionFromTask(Task t) {
String position = prod.term[1].toString();
if(position.contains("_")) {
try {
int posX = camera.minX + Util.discretization * Integer.valueOf(position.split("_")[0]);
int posY = camera.minY + Util.discretization * Integer.valueOf(position.split("_")[1]);

final int mappingCoordinateOfProductX = Integer.valueOf(position.split("_")[0]);
final int mappingCoordinateOfProductY = Integer.valueOf(position.split("_")[1]);
final double[] mappedCoordinate = Crossing.hexagonMapping.calcPositionOfHexagon(mappingCoordinateOfProductX, mappingCoordinateOfProductY);

int posX = camera.minX + (int)mappedCoordinate[0];//camera.minX + Util.discretization * Integer.valueOf(position.split("_")[0]);
int posY = camera.minY + (int)mappedCoordinate[1];//camera.minY + Util.discretization * Integer.valueOf(position.split("_")[1]);
//int id = 0; //Integer.valueOf(idStr.toString()); often a dep var
Entity pred;
if(type.toString().startsWith(car.toString())) {
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/com/opennars/applications/crossing/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,19 @@
public class Util {

public static Random rnd = new Random();
public static final int discretization =10;



public static int discretization = 10;

public static double distance(double posX, double posY, double posX2, double posY2) {
double dx = posX - posX2;
double dy = posY - posY2;
return Math.sqrt(dx * dx + dy * dy);
}

public static String positionToTerm(int X, int Y) {
int posX = X / discretization;
int posY = Y / discretization;
return posX + "_" + posY;
public static String positionToTerm(int x, int y) {
final Vec2Int pos = Crossing.hexagonMapping.map(x, y);
return pos.x + "_" + pos.y;
}

public static float truthToValue(TruthValue truth) {
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/com/opennars/applications/crossing/Vec2Int.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* The MIT License
*
* Copyright 2018 The OpenNARS authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.opennars.applications.crossing;

public class Vec2Int {
public int x;
public int y;

public Vec2Int(final int x, final int y) {
this.x = x;
this.y = y;
}

@Override
public int hashCode() {
return x * 5000 + y;
}

@Override
public boolean equals(Object other) {
return other instanceof Vec2Int && x == ((Vec2Int)other).x && y == ((Vec2Int)other).y;
}
}