Colorize Objects - java

My problem is that I need for a graphical output of an algorithm a method to colorize all the objects I have. So I wrote this one here:
int[] next_color = {0x70,0x00,0x00};
private int max_co = 0xF0;
private int next_c = 0x01;
private int step = 0x10;
public Color getNextColor(){
next_color[next_c%3]%=max_co;
next_color[(next_c++)%3]+=step;
return new Color(next_color[0], next_color[1], next_color[2]);
}
What I was thinking while writing this: I found out that colors under #707070 will appear mostly like black, so it does not make any sense to use them. Also I found out that only steps with greater than 0x10 are (really good) recognized by the eye. But now I have only red and some blue objects arround (when using small amounts of objects) - so it looks a little bit like crap. Is there a good way to generate a new color, which can be good differed from the previous and next one?

HSV/HSL color geometries should be easier to use in such algorithms.

Related

What would be the best way to let Box2D handle collision at different (simulated) heights?

I'm working on a 2D top-down game but we are also using a Z coordinate. This is to simulate the effect of 3D/height. Now the player can walk under a bridge and also over a bridge. The next thing I did was manage collision at these different heights. I got it working that the player collides with the bridge edge when he walks over it, but when he walks under it, he doesn't collide. But I'm not sure if I've done this correctly.
The way I'm currently doing it, is using different bits for different heights:
public static final short ENTITY_Z0_BIT = 1;
public static final short ENTITY_Z1_BIT = 2;
public static final short ENTITY_Z2_BIT = 4;
public static final short COLLISION_Z0_BIT = 8;
public static final short COLLISION_Z1_BIT = 16;
public static final short COLLISION_Z2_BIT = 32;
And then a collision body on Z 1 (or second floor) will have the following filter:
fixtureDef.filter.categoryBits = COLLISION_Z1_BIT;
fixtureDef.filter.maskBits = ENTITY_Z1_BIT;
fixtureDef.filter.groupIndex = (short) 0;
This will mean the wall on the second floor will only collide with an entity on the second floor, because I also change the entity's filter depending on if their Z coordinate increases/decreases. This means I have to add more bits if I need a higher simulated Z-axis and it also doesn't allow for dynamic z-axis handling as I will need to do a big switch-case or if-statements for every Z-axis and then set the filter accordingly.
My second option was setting the groupIndex to be the same as the collision/entity's z-axis + 1. If a collision body and an entity body both have groupIndex 1, they will ALWAYS collide. However if the entity moves up on the z-axis, it's groupIndex will be set to 2 and thus it won't collide anymore. This also seemed like a nice and more dynamic idea. But then anything that needs to collide with a wall or entity, automatically also collides with all the other bodies that need the same groupIndex for the same reason. This doesn't allow for a lot of flexibility.
Which of these 2 options is best? And if there's another better way to handle this, do let me know!

How do you generate random patterns and convert them to an image?

I am currently making a game. I have multiple users and for each user I have a id (UUID). I was hoping to use this id as like a key to generate a random pattern then to a image for that player.
boolean[] booleanPlayerID = BSUtility.bytesToBooleans(playerID.getBytes(UTF_8));
WritableImage image = new WritableImage(50, 50);
PixelWriter writer = image.getPixelWriter();
int booleanIndex = 0;
for(int y = 0; 50 > y; y++){
for(int x = 0; 50 > x; x++){
if(booleanIndex >= booleanPlayerID.length){
booleanIndex = 0;
}
if(booleanPlayerID[booleanIndex]){
writer.setColor(x, y, Color.BLACK);
}
booleanIndex++;
}
}
Assuming the image is going to be 50x50... What I have been doing is I take the player's id, convert to boolean array, cycle through image pixels, also cycle through boolean array, if the boolean value is true then set the pixel color to black. The if statement is to avoid indexing out of bounds (I know I should probably convert the boolean array to be the same length as how many pixels are in the image)...
This does produce some what of a pattern, but to be honest it is a really shitty pattern. The patterns generally tend to be very similar to one another. I was wondering if someone could provide tips/example of how to do this better. With my little research I really couldn't find much. I did however find patterns that I am hoping to somewhat obtain, I believe they are called tangle patterns. I would really hope to have each user have their own unique image/pattern but obvious it is fine if they are somewhat similar but not same. I don't know if this is possible to be honest.
If you need a complete example I can replace the code above. Although the code above should be pretty straight forward to making it a full example (only things missing is generating the userID and converting it to boolean array from bytes).
What you are talking about is known as IDENTICON.
I actually don't know how they work but i know that they use some sort of hashing with the UNIQUE TEXT and generate image with that hash.
Here are two website i found who do provide source code so if you are interested you can look into the code and extract any useful information from there.
http://identicon.net/
https://jdenticon.com/
PS. Code are in JavaScript and on one website its in .NET and PHP too.

OpenCV - Image Processing Technique for better reading of embossed characters (e.g in credit cards,plate number)

So, I've already searched a lot of articles about image processing of plate numbers since it is basically just the same as reading embossed characters. But my problem is if the embossed characters color is the same as the color of the card it will be hard to recognize if I use Adaptive Threshold. Do you guys have any idea on how I will achieve my goal of reading embossed characters? Or if there's any article about it, it would be nice. Here's my sample image
I've read this article but don't really know how to translate it into actual code.
Here's my sample image
You can try out this code. It's in python but all the opencv methods are, from my personal experience, quite the same as Java.
EDIT:
In your case, you can skip all the OCR enhancement methods used here. Let me describe the process you can use:
In python using the link above:
First, apply a color conversion to grayscale:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Then:
apply a tophat (whitehat) morphological operator to find light
regions against a dark background.
rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 3))
tophat = cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, rectKernel)
You may need to use a threshold before doing this because your image has not many contrast.
gradX = cv2.morphologyEx(gradX, cv2.MORPH_CLOSE, rectKernel)
thresh = cv2.threshold(gradX, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
Apply a second closing operation to the binary image to make regions appear and close gaps between credit card number regions
sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, sqKernel)
Then find contours of your card numbers regions
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
locs = []
You will get an array of regions you will be able to extract using this thread
In Java:
I previously wrote something similar in java, you should have something similar to this (ImgTransformation is one of my class that simplifies some opencv parameters, it basically does the same as Imgproc, go check the doc here):
Image transformation:
ImgTransformation.adjustConvertColor(tempMat,tempMat,Imgproc.COLOR_BGR2GRAY);
Imgproc.adaptiveThreshold(tempMat,tempMat,255,Imgproc.ADAPTIVE_THRESH_MEAN_C,Imgproc.THRESH_BINARY,31,40);
ImgTransformation.gaussianBlur(tempMat,tempMat,5,5,2);
Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2*1 + 1, 2*1+1));
Imgproc.dilate(tempMat,tempMat,element);
Imgproc.erode(tempMat,tempMat,element);
ImgTransformation.adjustBinary(tempMat,tempMat,205,255,Imgproc.THRESH_BINARY_INV);
Segmentation:
Mat structuringElements = ImgSegmentation.getStructuringElement(Imgproc.MORPH_RECT, new Size(5, 3));
Imgproc.dilate(tempMat,tempMat,structuringElements,new Point(-1,-1),16);
Then extract regions:
List<MatOfPoint> contours = ImgSegmentation.findContours(tempMat, tempMat);
You just have then to extract using submat() and you're good !
Play with some numeric parameters to tune the result in your own case as my project is more about scanned documents than credit card numbers.
Good luck !

Generating knots procedurally (and visualizing them)

I'm looking for an algorithm that provides a way to generate graphical representations of knots (either 2D or 3D, but the former, with vector graphics is preferable).
I've seen many links to knot theory in general, spanning from punctual references to general information.
Before trying to devise something from scratch by myself, I'd like to know about the existence of some existing software that lets you not only represent them (in memory) but visualize in some of their graphical representations (there are many). It could come in the form of a library, or a simple function, or even a pseudocode algorithm that tries to specify how to properly draw a know on screen.
As the previous link suggests, there is a package in Wolfram Mathematica, named KnotTheory that does that (in an almost complete way). However, it is not portable, nor free software and accessing its modules would be very cumbersome for me (a free implementation in Java, just to name a language, but each language is fine, would be be ideal from the portability and openness perspectives).
I've seen that many softwares are available (but most of them are old and not reachable or usable).
Do you have some good pointers to share?
UPDATE:
Since two votes to close this question have appeared, I am restating it in a more pragmatic and clear way: are there algorithms to draw and generate knots? has something been already implemented
UPDATE 2 (for reopening)
The graphical representation could be a 3D rendered object or a 2D svg graphics (I am abstracting from it since I am looking forward a programming language as Processing (or the same Mathematica itself) that provides you the primitives to draw curves (splines, beziers, etc) on screen (and then export them to raster or vector graphics).
The algorithm shall take one knot parametrization as input (ie, if we are talking about knots described by their crossing properties, their values is what is needed), returning one of the graphical representation above (ie even a sequence of points in a 2d space). That is, any parametrization is fine, my objective is just to get introspection on how to draw knots so to get ONE algorithm that does that in a particular way, leading to a particular representation, would be fine (Mathematica's lib seems to be able to draw it in so many representations).
Something like this?
void setup() {
size(300, 300, P3D);
}
void draw() {
background(36, 10, 28);
int f = frameCount%360;
translate(width/2, height/2);
if (frameCount >= 360 && frameCount <= 1080 )
rotateY(radians(f));
stroke(0);
drawKnot(40);
translate(knotX(f)*40, knotY(f)*40, knotZ(f)*40);
noStroke();
fill(180,50,145);
sphere(10);
}
void drawKnot(float sz) {
stroke(200);
for (int i = 0; i < 360; i++) {
point(knotX(i)*sz, knotY(i)*sz, knotZ(i)*sz);
}
}
float knotX(int n) {
return sin(radians(n)) + 2*sin(radians(n*2));
}
float knotY(int n) {
return cos(radians(n)) - 2*cos(radians(n*2));
}
float knotZ(int n) {
return sin(radians(n*3))*-1;
}
Wolfram Mathematica has basic knot theory, including visualization, built in:
http://reference.wolfram.com/language/ref/KnotData.html
This webpage from the software documentation contains many examples of standard knot-theoretic visualization and computation that can be done with Mathematica.

Designing a hand history class for Texas Hold'em in Java

I am trying to come up with a Java hand history class for Texas Hold'em and wanted to bounce an idea off here.
Requirements are that every action is stored and there is an efficient way to traverse each HandHistory object (which would represent a single played hand) to match common 'lines' like the standard continuation bet (i.e. the preflop raiser who was in late position preflop and probably is in position postflop is checked to and then makes a 75%ish pot bet).
Ignore for the moment that the definitions for each of the lines is fuzzy at best. As a first stab, I was thinking of organizing it like so:
public class HandHistory {
private Integer handnumber;
//in case we saw things at showdown, put them here
private HashMap<Player,String> playerHands;
private String flopCards;
private String turnCard;
private String riverCard;
private HashMap<BetRound,LinkedHashMap<Integer,ArrayList<PlayerAction>>> actions;
}
So, for each betround we store a linked hashmap whose keys are integers that are the offsets from first position to act for that betround, so preflop UTG is 0.
We generate the actions in position order already, so use a linked hashmap so we can iterate nicely later and skip over positions that are sitting out,etc.
Each arraylist will contain the actions that that position had in that betround. Most of the time this array will have one element, but in cases like, limps and then calls, it will have two.
Can anyone see a better data structure to use for this?
small change, since holdem has a fixed number of betting rounds, maybe
private HashMap<BetRound,LinkedHashMap<Integer,ArrayList<PlayerAction>>> actions;
could just be:
private LinkedHashMap<Integer,ArrayList<PlayerAction>>[] actions= new ... ;
also, here's a couple of books that may be of interest:
http://www.amazon.com/Poker-strategy-Winning-game-theory/dp/0399506691
http://www.amazon.com/Mathematics-Poker-Bill-Chen/dp/1886070253
class Card {
// ??? probably just an int (0 to 51), but certainly not a String.
// Instead of using class Card below, directly using an int woulb be OK.
}
class Hand {
Set<Card> hand; // size 2
}
class Draw { // not sure of the exact poker name for the 5 cards
Set<Card> flop; // size 3
Card turn;
Card river;
}
class Bet {
Player player;
// or maybe "Double" instead; then amount == null means player dropping out.
// or use 0.0 to mean dropped out.
double amount;
}
class BettingRound {
// Includes initial "entry" and all subsequent calls.
// Should include players dropping out also.
List<Bet> bets;
}
class Game {
Draw draw;
Map<Player, Hand> hands;
// rounds 0 and 1 are special since turn and river are not shown
List<BettingRound> rounds;
}
You should also know how much money each player has, I guess. You could keep track of that with a third field (total cash before the bet) in class Bet.
After thinking about it for a bit more, I think I have answered my own question. I've decided not to try to both store every action in a tabular way and try to get quick lookup of frequency counts for betting lines in the same data structure.
Instead, I am going to use my class above as the database-like storage to disk and use a DAG for the betting lines where the edges are tuples of {action, position, players ahead} and the vertices are frequency counts. I think a DAG is correct over a trie here since I do want multiple edges coming into a vertex since lines with common suffix are considered close.

Categories