Links und Funktionen
Sprachumschaltung

Navigationspfad


Inhaltsbereich

Lösung6-3

Java source code icon LoesungAufgabe6_3.java — Java source code, 9 KB (9892 bytes)

Dateiinhalt

import java.awt.Color;
import java.awt.Graphics2D;

public class LoesungAufgabe6_3 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final int xsize = 500;
		final int ysize = 500;		

		// ThumbsUp
		//int[][] voronoi = { { 0, 0, 0 }, { 248, 245, -101775 }, { 171, 341, -1 }, { 247, 346, -1 }, { 328, 342, -1 }, { 257, 144, -16777216 }, { 278, 4, -1 }, { 274, 58, -16316665 }, { 305, 57, -1 }, { 360, 164, -1 }, { 220, 68, -1 }, { 170, 137, -1 }, { 62, 332, -16711423 }, { 86, 364, -65794 }, { 62, 379, -16711423 }, { 40, 373, -1 }, { 22, 355, -16514044 }, { 10, 339, -197380 }, { 5, 375, -1 }, { 14, 320, -16448251 }, { 34, 301, -65794 }, { 7, 296, -1 }, { 106, 294, -65794 }, { 79, 285, -16316665 }, { 53, 265, -1 }, { 104, 225, -1 }, { 106, 256, -16250872 }, { 76, 242, -1 }, { 122, 270, -131587 }, { 196, 183, -16711423 }, { 148, 183, -1 }, { 322, 193, -16711423 }, { 378, 228, -1 }, { 346, 250, -16711423 }, { 337, 287, -65794 }, { 216, 298, -15857393 }, { 280, 297, -16055805 }, { 216, 278, -248777 }, { 274, 286, -1177328 }, { 172, 286, -65794 }, { 208, 324, -1 }, { 288, 316, -1 }, { 193, 305, -65794 }, { 312, 296, -1 }, { 226, 223, -217428 }, { 277, 227, -241584 }, { 280, 176, -16645888 }, { 178, 230, -16711423 }, { 147, 255, -16711423 }, { 142, 223, -65794 }, { 101, 276, -65794 }, { 86, 264, -16316665 }, { 113, 345, -16711423 }, { 132, 301, -16579837 }, { 141, 279, -16579837 }, { 154, 313, -1 }, { 138, 377, -65794 }, { 117, 386, -16579837 }, { 117, 430, -16316665 }, { 87, 391, -65794 }, { 56, 405, -1 }, { 90, 437, -1 }, { 129, 466, -1 }, { 183, 430, -1 }, { 167, 419, -16448251 }, { 142, 403, -65794 }, { 182, 379, -1 }, { 200, 402, -16185079 }, { 222, 416, -1 }, { 252, 417, -1 }, { 286, 428, -1 }, { 306, 447, -1 }, { 347, 468, -1 }, { 355, 439, -16316665 }, { 313, 409, -16316665 }, { 274, 389, -16579837 }, { 232, 388, -16514044 }, { 217, 365, -1 }, { 278, 358, -1 }, { 346, 399, -131587 }, { 311, 376, -1 }, { 259, 107, -16711423 }, { 247, 34, -1 }, { 290, 35, -1 }, { 322, 108, -1 }, { 298, 132, -16711423 }, { 372, 282, -16711423 }, { 401, 301, -1 }, { 377, 342, -16645630 }, { 376, 394, -16777216 }, { 355, 355, -1 }, { 358, 324, -131587 }, { 376, 311, -16250872 }, { 402, 339, -1 }, { 400, 397, -1 }, { 379, 447, -1 }, { 262, 254, -176306 }, { 219, 262, -305322 }, { 214, 243, -296070 }, { 286, 245, -315087 }, { 264, 212, -561297 }, { 321, 227, -16645630 }, { 239, 178, -16776703 }, { 246, 211, -290415 }, { 244, 264, -44205 }, { 233, 319, -131587 }, { 260, 320, -1 }, { 194, 272, -15924987 }, { 303, 277, -16251124 }, { 324, 283, -65794 }, { 286, 265, -844775 }, { 203, 257, -960168 }, { 448, 151, -16645630 }, { 439, 219, -16711423 }, { 484, 152, -1 }, { 437, 105, -1 }, { 414, 168, -1 }, { 470, 117, -1 }, { 496, 230, -1 }, { 455, 258, -1 }, { 425, 256, -1 }, { 406, 254, -16053493 }, { 422, 238, -16053493 }, { 402, 208, -1 }, { 394, 230, -1 }, { 414, 276, -1 }, { 382, 250, -65794 }, { 481, 204, -16711423 }, { 467, 211, -16382458 }, { 458, 197, -15066598 }, { 470, 195, -16777216 }, { 459, 212, -16514044 }, { 468, 210, -16711423 }, { 499, 189, -65794 }, { 488, 182, -16316665 }, { 438, 246, -1 }, { 410, 222, -65794 }, { 396, 244, -263173 }, { 417, 265, -1 }, { 359, 193, -1 }, { 246, 58, -65794 }, { 41, 45, -1 }, { 17, 13, -1 }, { 427, 42, -1 }, { 58, 122, -1 }, { 56, 461, -1 }, { 229, 477, -1 }, { 470, 445, -1 }, { 375, 468, -1 }, { 327, 389, -1 }, { 271, 412, -1 }, { 265, 36, -1 }, { 288, 51, -65794 }, { 331, 137, -65794 }, { 168, 163, -1 }, { 316, 158, -16645630 }, { 139, 346, -131587 }, { 128, 326, -16514044 }, { 108, 454, -1 }, { 136, 411, -65794 }, { 249, 358, -1 }, { 144, 430, -16382458 }, { 155, 454, -1 }, { 107, 310, -65794 } }; 
		
		// Sesame
		int[][] voronoi = { { 0, 0, -15001046 }, { 342, 173, -1703933 }, { 138, 364, -2927072 }, { 151, 398, -2927074 }, { 156, 446, -2728811 }, { 177, 437, -1610847 }, { 125, 459, -5108183 }, { 150, 468, -4846040 }, { 173, 462, -3271369 }, { 198, 450, -4583896 }, { 211, 426, -4385489 }, { 214, 380, -3555830 }, { 209, 351, -2831864 }, { 241, 325, -2039527 }, { 37, 391, -3620858 }, { 89, 333, -16711659 }, { 169, 307, -16711660 }, { 119, 282, -2895857 }, { 49, 230, -16646123 }, { 87, 211, -16711666 }, { 54, 202, -16711666 }, { 73, 185, -16777204 }, { 100, 169, -16711664 }, { 68, 147, -1 }, { 29, 177, -1 }, { 12, 213, -1 }, { 122, 146, -1 }, { 150, 170, -1 }, { 130, 167, -15527375 }, { 141, 195, -16448490 }, { 174, 202, -1 }, { 23, 21, -1 }, { 99, 305, -1841962 }, { 73, 318, -3423295 }, { 65, 336, -3885138 }, { 52, 311, -2633184 }, { 71, 297, -3224050 }, { 96, 284, -3422199 }, { 54, 283, -16777216 }, { 75, 269, -16777214 }, { 97, 261, -16776950 }, { 47, 263, -2961899 }, { 75, 251, -3355128 }, { 123, 230, -2894586 }, { 97, 238, -3025147 }, { 152, 222, -787166 }, { 128, 245, -16711680 }, { 152, 242, -16777206 }, { 138, 265, -2961397 }, { 178, 263, -2302447 }, { 146, 295, -3094339 }, { 164, 284, -2764611 }, { 188, 291, -2107186 }, { 192, 315, -2567232 }, { 147, 318, -3424838 }, { 172, 327, -3555656 }, { 153, 346, -2729182 }, { 76, 352, -4147532 }, { 91, 353, -3819855 }, { 114, 325, -3619404 }, { 130, 319, -3820205 }, { 120, 352, -2991838 }, { 134, 339, -2728414 }, { 109, 369, -2663904 }, { 86, 384, -3621870 }, { 99, 427, -3884793 }, { 126, 398, -2663140 }, { 168, 376, -2992869 }, { 93, 472, -5267700 }, { 151, 493, -5860342 }, { 121, 488, -6253564 }, { 186, 489, -5071602 }, { 216, 469, -4410351 }, { 237, 431, -2763239 }, { 200, 397, -4478194 }, { 173, 396, -3058147 }, { 12, 262, -1 }, { 4, 326, -1 }, { 9, 454, -1 }, { 57, 494, -1 }, { 87, 498, -1 }, { 242, 490, -1 }, { 269, 439, -1 }, { 29, 199, -16710897 }, { 55, 166, -16448995 }, { 23, 233, -15001046 }, { 241, 266, -1 }, { 208, 226, -1 }, { 213, 281, -393439 }, { 300, 322, -1 }, { 277, 285, -1 }, { 289, 384, -1 }, { 260, 62, -1 }, { 284, 22, -1 }, { 319, 5, -1 }, { 370, 6, -5 }, { 421, 12, -1 }, { 463, 46, -1 }, { 474, 94, -1 }, { 475, 130, -1 }, { 284, 46, -16711674 }, { 314, 27, -16250084 }, { 346, 14, -15723491 }, { 390, 19, -15261408 }, { 432, 39, -14274752 }, { 446, 71, -15592416 }, { 441, 103, -16447214 }, { 321, 113, -16776960 }, { 393, 145, -16777206 }, { 392, 169, -1913440 }, { 412, 160, -929374 }, { 374, 131, -1519706 }, { 401, 130, -989252 }, { 310, 135, -1781854 }, { 300, 120, -1975896 }, { 317, 99, -1386326 }, { 318, 87, -48381 }, { 342, 109, -333389 }, { 340, 127, -1715809 }, { 348, 155, -1441788 }, { 330, 156, -1441788 }, { 366, 173, -1441532 }, { 319, 61, -16777212 }, { 375, 87, -16777214 }, { 411, 91, -16776954 }, { 228, 103, -40190 }, { 467, 184, -39933 }, { 465, 207, -40960 }, { 491, 169, -3 }, { 490, 230, -1 }, { 447, 250, -1 }, { 193, 79, -1 }, { 191, 145, -1 }, { 274, 121, -48384 }, { 289, 156, -312832 }, { 286, 92, -47359 }, { 354, 98, -179712 }, { 387, 111, -113152 }, { 415, 119, -46844 }, { 402, 117, -243200 }, { 388, 125, -793938 }, { 356, 129, -368618 }, { 367, 114, -46848 }, { 389, 202, -508160 }, { 426, 185, -113152 }, { 436, 152, -46336 }, { 243, 198, -44279 }, { 232, 162, -44800 }, { 263, 239, -40192 }, { 292, 259, -632320 }, { 323, 269, -173822 }, { 364, 265, -175872 }, { 402, 246, -43776 }, { 324, 293, -1 }, { 371, 289, -1 }, { 415, 270, -65794 }, { 313, 222, -1292917 }, { 333, 189, -2031358 }, { 369, 216, -179967 }, { 295, 181, -443648 }, { 270, 195, -2359292 }, { 289, 230, -2490100 }, { 316, 246, -2031605 }, { 361, 240, -2097142 }, { 425, 210, -47872 }, { 496, 203, -3 }, { 200, 182, -1 }, { 201, 160, -1 }, { 224, 57, -1 }, { 189, 111, -1 }, { 38, 328, -3356907 }, { 61, 368, -4015339 }, { 40, 348, -3489006 }, { 215, 318, -2500848 }, { 226, 245, -1 }, { 277, 405, -1 }, { 257, 464, -1 }, { 226, 495, -1 }, { 210, 499, -1 }, { 246, 479, -1 }, { 236, 374, -2764522 }, { 352, 188, -1769214 }, { 321, 173, -2162432 }, { 346, 150, -1310713 }, { 434, 171, -46079 }, { 405, 191, -114431 }, { 384, 193, -311552 }, { 327, 225, -375940 }, { 302, 211, -115328 }, { 340, 246, -2621435 }, { 301, 244, -2293748 }, { 345, 269, -175872 }, { 348, 296, -1 }, { 356, 216, -50175 }, { 304, 190, -247808 }, { 275, 213, -2359288 }, { 253, 221, -41472 }, { 365, 40, -16777208 }, { 7, 7, -1 }, { 438, 447, -1 }, { 117, 431, -4214269 }, { 168, 441, -2005345 }, { 207, 438, -4189651 }, { 230, 450, -3224542 }, { 427, 223, -305919 } }; 
		System.out.println("Length: " + voronoi.length);
		
		CanvasFrame frame = new CanvasFrame("Voronoi", xsize, ysize);
		
		for (int y = 0; y < ysize; y++) {
			for (int x = 0; x < xsize; x++) {				
				int minIndex = 0;
				int distance = xsize * xsize + ysize * ysize + 1; // Initialisierung mit maximaler Distanz
				for (int index = 0; index < voronoi.length; index++) {
					int px = voronoi[index][0];
					int py = voronoi[index][1];
					int distToIndex = (x-px)*(x-px)+(y-py)*(y-py); // Ziehen der Wurzel zum Längen vergleich ist überflüssig, da die Wurzelfunktion monton ist.
					if (distToIndex < distance) {
						minIndex = index;
						distance = distToIndex;
					}
					// Wenn man dem Programm "zugucken" möchte, 
					// kann man einfach die nächste Zeile auskommentieren:
					// drawPoint(frame, x, y, voronoi[minIndex][2]);
				}
				drawPoint(frame, x, y, voronoi[minIndex][2]);
			}
		}	
		// Hilfreiche Ausgabe, fals es schiefgeht
		System.out.println("Finished painting!!!");
	}
	
	public static void drawPoint(CanvasFrame frame, int x, int y, int color){
		/*
		 * Hinweis:
		 * Diese Funktion ist sehr ineffizient. 
		 * Wenn Sie das Programm stark beschleunigen wollen,
		 * so sollten Sie dafür Sorgen, das die beiden Aufrufe 
		 * 	  frame.getGraphics()
		 * und
		 * 	  frame.repaint();
		 * im gesamten Programmablauf höchstens einmal stattfinden,
		 * und nicht wie hier für jeden Punkt durchgeführt werden. 
		 */
		Graphics2D canvas = frame.getGraphics(); 
		Color c = new Color(color);
	    canvas.setColor(c);
	    canvas.fillRect(x, y, 1, 1);
		frame.repaint();
	}
}


Artikelaktionen


Funktionsleiste