简体   繁体   中英

Convert Points to textual representation

Let's say I have two Points

new Point(80,40);
new Point(40,80);

And I want to convert them to end up as one string, a String representation.

01
10

Where 1 is a point, and 0 is empty space. You can see here that the string is magnified 40x, and each point has a width and height of 40. All points will be multiples of 40.

I tried to make something up with StringBuilder but I don't know where to start, given that I can only insert a character given the length of a string, not the height(or y value).

Here is a another example.

 new Point(120,80);
 new Point(80,240);
 new Point(40,80);

turns into

 010
 001
 000
 000
 000
 010

I would preferably like a solution that is efficient, I have thousands of Points. No code is required for a solution, just an idea of how this might be implemented.

However, data is actually stored in a hashmap containing the character that is supposed to be written(in the above example all 1) as the key and the point as the value.

Example for Hashmap

     Value             Key
 new Point(40,40)      'a'
 new Point(80,80)      'b'

Which would result in

 a0
 0b

Here is a solution that uses memory, but is nevertheless computationally efficient:

  1. Create a 2D boolean array to represent your "map", initialized to all- false .

  2. Iterate through your points, divide coordinates by 40, use this to index into the array, and set to true .

  3. Iterate over your 2D array, printing 1 or 0 for each element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM