Draw Pixels in a square from inside out - Java -
i want draw square of pixels pending on how many items in array. square represents array amount small squares represent small arrays , large squares represent large arrays. finding difficult conceptualize how go this?
edit: using java 2d.
the spiral starts @ 1 , advances anti-clockwise towards outside of square (i.e. 2,3,4,5 etc). each square can represented amount of data square represents.
public class test { enum direction { right, up, left, down } public static void main(string... args) throws ioexception { bufferedimage image = new bufferedimage(100, 100, bufferedimage.type_int_argb); int rgb = color.black.getrgb(); point p = new point(50, 50); direction d = direction.right; int currentsegmentlength = 1; (int = 0; < 100; += 2) { paintsegment(image, rgb, p, d, currentsegmentlength); d = nextsegmentdirection(d); paintsegment(image, rgb, p, d, currentsegmentlength); d = nextsegmentdirection(d); currentsegmentlength++; } imageio.write(image, "png", new file("test.png")); } private static void paintsegment(bufferedimage image, int rgb, point p, direction d, int currentsegmentlength) { (int s = 0; s < currentsegmentlength; s++) { image.setrgb(p.x, p.y, rgb); switch (d) { case right: p.x++; break; case up: p.y--; break; case left: p.x--; break; case down: p.y++; break; } } } private static direction nextsegmentdirection(direction d) { switch (d) { case right: return direction.up; case up: return direction.left; case left: return direction.down; case down: return direction.right; default: throw new runtimeexception("never here"); } } }
Comments
Post a Comment