c# - Getting Rectangles stacked vertically to horizontally but keeping shape -
i have input done correctly rectangles stacked vertically , have different colors each time new rectangle drawn, struggling output bottom pic. please can put me on right path.
//list strore randomly generated rectangles list<rectangle> rectanglecollection = new list<rectangle>(); //counts amount of time rectangle needs drawn int count = 0; public static random ran = new random(); void createrectangle() { int tallestrectangle = 0; ; int prevrecy = 0; graphics graphic = picturebox1.creategraphics(); solidbrush brush; foreach (rectangle rect in rectanglecollection) { if (rect.height > tallestrectangle) tallestrectangle = rect.height; } foreach (rectangle rect in rectanglecollection) { graphic.fillrectangle(brush = new solidbrush(color.fromargb(ran.next(1, 255), ran.next(1, 255), ran.next(1, 255))), new rectangle(rect.x + prevrecy, (tallestrectangle - rect.height), rect.width, rect.height)); prevrecy += rect.width; } } private void button1_click(object sender, eventargs e) { count = int.parse(textbox1.text); (int = 0; < count; i++) { getrandomrectangle(); } createrectangle(); } void getrandomrectangle() { graphics graph = this.creategraphics(); int x = 0; int y = 0; int width = ran.next(20, 100); int height = ran.next(30, 150); rectangle rec1 = new rectangle(x, y, width, height); rectanglecollection.add(rec1); }
for each rectangle, ordered shortest tallest,
- make horizontal rectangle of height, starting left of rectangles taller that, ending @ right of rectangles right of that.
whenever you're creating horizontal rectangle place on top of highest horizontal rectangle below (in terms of hiorizontal position) 1 you're creating.