■ 탈출 시간 다항식 프랙탈(Escape Time Polynomial Fractal)을 그리는 방법을 보여준다.
▶ GraphicExtension.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; namespace TestProject { /// <summary> /// 그래픽 확장 /// </summary> public static class GraphicExtension { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 대시 상자 그리기 - DrawDashedBox(graphics, color1, color2, thickness, dashSize, point1, point2) /// <summary> /// 대시 상자 그리기 /// </summary> /// <param name="graphics">그래픽스</param> /// <param name="color1">색상 1</param> /// <param name="color2">색상 2</param> /// <param name="thickness">두께</param> /// <param name="dashSize">대시 크기</param> /// <param name="point1">포인트 1</param> /// <param name="point2">포인트 2</param> public static void DrawDashedBox(this Graphics graphics, Color color1, Color color2, float thickness, float dashSize, Point point1, Point point2) { Rectangle rectangle = point1.ToRectangle(point2); using(Pen pen = new Pen(color1, thickness)) { graphics.DrawRectangle(pen, rectangle); pen.DashPattern = new float[] { dashSize, dashSize }; pen.Color = color2; graphics.DrawRectangle(pen, rectangle); } } #endregion #region 사각형 구하기 - ToRectangle(point1, point2) /// <summary> /// 사각형 구하기 /// </summary> /// <param name="point1">포인트 1</param> /// <param name="point2">포인트 2</param> /// <returns>사각형</returns> public static Rectangle ToRectangle(this Point point1, Point point2) { int x = Math.Min(point1.X, point2.X); int y = Math.Min(point1.Y, point2.Y); int width = Math.Abs(point1.X - point2.X); int height = Math.Abs(point1.Y - point2.Y); return new Rectangle(x, y, width, height); } #endregion #region 사각형 구하기 - ToRectangle(point1, point2) /// <summary> /// 사각형 구하기 /// </summary> /// <param name="point1">포인트 1</param> /// <param name="point2">포인트 2</param> /// <returns>사각형</returns> public static RectangleF ToRectangle(this PointF point1, PointF point2) { float x = Math.Min(point1.X, point2.X); float y = Math.Min(point1.Y, point2.Y); float width = Math.Abs(point1.X - point2.X); float height = Math.Abs(point1.Y - point2.Y); return new RectangleF(x, y, width, height); } #endregion #region 이미지 저장하기 - SaveImage(image, filePath) /// <summary> /// 이미지 저장하기 /// </summary> /// <param name="image">이미지</param> /// <param name="filePath">파일 경로</param> public static void SaveImage(this Image image, string filePath) { string fileExtension = Path.GetExtension(filePath); switch(fileExtension.ToLower()) { case ".bmp" : image.Save(filePath, ImageFormat.Bmp ); break; case ".exif" : image.Save(filePath, ImageFormat.Exif); break; case ".gif" : image.Save(filePath, ImageFormat.Gif ); break; case ".jpg" : case ".jpeg" : image.Save(filePath, ImageFormat.Jpeg); break; case ".png" : image.Save(filePath, ImageFormat.Png ); break; case ".tif" : case ".tiff" : image.Save(filePath, ImageFormat.Tiff); break; default : throw new NotSupportedException($"알 수 없는 파일 확장자 {fileExtension}"); } } #endregion } } |
▶ BitmapHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 |
using System; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 비트맵 헬퍼 /// </summary> public class BitmapHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Enumeration ////////////////////////////////////////////////////////////////////////////////////////// Public #region 워프 연산 - WarpOperation /// <summary> /// 워프 연산 /// </summary> public enum WarpOperation { /// <summary> /// Identity /// </summary> Identity, /// <summary> /// FishEye /// </summary> FishEye, /// <summary> /// Twist /// </summary> Twist, /// <summary> /// Wave /// </summary> Wave, /// <summary> /// SmallTop /// </summary> SmallTop, /// <summary> /// Wiggles /// </summary> Wiggles, /// <summary> /// DoubleWave /// </summary> DoubleWave } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Class ////////////////////////////////////////////////////////////////////////////////////////// Public #region 필터 - Filter /// <summary> /// 필터 /// </summary> public class Filter { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 커널 배열 /// </summary> public float[,] KernelArray; /// <summary> /// 가중치 /// </summary> public float Weight; /// <summary> /// 오프셋 /// </summary> public float Offset; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 정규화하기 - Normalize() /// <summary> /// 정규화하기 /// </summary> public void Normalize() { Weight = 0; for(int row = 0; row <= KernelArray.GetUpperBound(0); row++) { for(int column = 0; column <= KernelArray.GetUpperBound(1); column++) { Weight += KernelArray[row, column]; } } } #endregion #region 총합이 0이 되는 중앙 커널 상관계수 설정하기 - SetCenterKernelCoefficientForZeroTotal() /// <summary> /// 총합이 0이 되는 중앙 커널 상관계수 설정하기 /// </summary> public void SetCenterKernelCoefficientForZeroTotal() { float total = 0; int rowCount = KernelArray.GetUpperBound(0); int columnCount = KernelArray.GetUpperBound(1); for(int row = 0; row <= rowCount; row++) { for(int column = 0; column <= columnCount; column++) { total += KernelArray[row, column]; } } int middleRow = (int)(KernelArray.GetUpperBound(0) / 2); int middleColumn = (int)(KernelArray.GetUpperBound(1) / 2); total -= KernelArray[middleRow, middleColumn]; KernelArray[middleRow, middleColumn] = -total; } #endregion } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 이미지 바이트 배열 /// </summary> public byte[] ImageByteArray; /// <summary> /// 행 크기 바이트 카운트 /// </summary> public int RowSizeByteCount; /// <summary> /// 픽셀 데이터 크기 /// </summary> public const int PixelDataSize = 32; /// <summary> /// 비트맵 /// </summary> public Bitmap Bitmap; #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 잠금 여부 /// </summary> private bool isLocked = false; /// <summary> /// 비트맵 데이터 /// </summary> private BitmapData bitmapData; #endregion #region 너비 - Width /// <summary> /// 너비 /// </summary> public int Width { get { return Bitmap.Width; } } #endregion #region 높이 - Height /// <summary> /// 높이 /// </summary> public int Height { get { return Bitmap.Height; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 엠보싱 필터 - EmbossingFilter /// <summary> /// 엠보싱 필터 /// </summary> public static Filter EmbossingFilter { get { return new Filter() { Weight = 1, Offset = 127, KernelArray = new float[,] { { -1, 0, 0 }, { 0, 0, 0 }, { 0, 0, 1 } } }; } } #endregion #region 엠보싱 필터 2 - EmbossingFilter2 /// <summary> /// 엠보싱 필터 2 /// </summary> public static Filter EmbossingFilter2 { get { return new Filter() { Weight = 1, Offset = 127, KernelArray = new float[,] { { 2, 0, 0 }, { 0, -1, 0 }, { 0, 0, -1 } } }; } } #endregion #region 5X5 가우시안 블러 필터 - BlurFilter5X5Gaussian /// <summary> /// 5X5 가우시안 블러 필터 /// </summary> public static Filter BlurFilter5X5Gaussian { get { Filter filter = new Filter() { Offset = 0, KernelArray = new float[,] { { 1, 4, 7, 4, 1 }, { 4, 16, 26, 16, 4 }, { 7, 26, 41, 26, 7 }, { 4, 16, 26, 16, 4 }, { 1, 4, 7, 4, 1 } } }; filter.Normalize(); return filter; } } #endregion #region 5X5 평균 블러 필터 - BlurFilter5X5Mean /// <summary> /// 5X5 평균 블러 필터 /// </summary> public static Filter BlurFilter5X5Mean { get { Filter filter = new Filter() { Offset = 0, KernelArray = new float[,] { { 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1 } } }; filter.Normalize(); return filter; } } #endregion #region 좌상단→우하단 모서리 탐지 필터 - EdgeDetectionFilterULtoLR /// <summary> /// 좌상단→우하단 모서리 탐지 필터 /// </summary> public static Filter EdgeDetectionFilterULtoLR { get { return new Filter() { Weight = 1, Offset = 0, KernelArray = new float[,] { { -5, 0, 0 }, { 0, 0, 0 }, { 0, 0, 5 } } }; } } #endregion #region 상단→하단 모서리 탐지 필터 - EdgeDetectionFilterTopToBottom /// <summary> /// 상단→하단 모서리 탐지 필터 /// </summary> public static Filter EdgeDetectionFilterTopToBottom { get { return new Filter() { Weight = 1, Offset = 0, KernelArray = new float[,] { { -1, -1, -1 }, { 0, 0, 0 }, { 1, 1, 1 } } }; } } #endregion #region 좌측→우측 모서리 탐지 필터 - EdgeDetectionFilterLeftToRight /// <summary> /// 좌측→우측 모서리 탐지 필터 /// </summary> public static Filter EdgeDetectionFilterLeftToRight { get { return new Filter() { Weight = 1, Offset = 0, KernelArray = new float[,] { { -1, 0, 1 }, { -1, 0, 1 }, { -1, 0, 1 } } }; } } #endregion #region 3X3 하이 패스 필터 - HighPassFilter3X3 /// <summary> /// 3X3 하이 패스 필터 /// </summary> public static Filter HighPassFilter3X3 { get { return new Filter() { Weight = 16, Offset = 127, KernelArray = new float[,] { { -1, -2, -1 }, { -2, 12, -2 }, { -1, -2, -1 } } }; } } #endregion #region 5X5 하이 패스 필터 - HighPassFilter5X5 /// <summary> /// 5X5 하이 패스 필터 /// </summary> public static Filter HighPassFilter5X5 { get { Filter filter = new Filter() { Offset = 127, KernelArray = new float[,] { { -1, -4, -7, -4, -1 }, { -4, -16, -26, -16, -4 }, { -7, -26, -41, -26, -7 }, { -4, -16, -26, -16, -4 }, { -1, -4, -7, -4, -1 } } }; filter.Normalize(); filter.Weight = -filter.Weight; filter.SetCenterKernelCoefficientForZeroTotal(); return filter; } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Public #region 잠금 여부 - IsLocked /// <summary> /// 잠금 여부 /// </summary> public bool IsLocked { get { return this.isLocked; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - BitmapHelper(bitmap) /// <summary> /// 생성자 /// </summary> /// <param name="bitmap">비트맵</param> public BitmapHelper(Bitmap bitmap) { Bitmap = bitmap; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 픽셀 매핑하기 - MapPixel(warpOperation, middleX, middleY, maximumR, targetX, targetY, sourceX, sourceY) /// <summary> /// 픽셀 매핑하기 /// </summary> /// <param name="warpOperation">워프 연산</param> /// <param name="middleX">중간 X</param> /// <param name="middleY">중간 Y</param> /// <param name="maximumR">최대 R</param> /// <param name="targetX">타겟 X</param> /// <param name="targetY">타겟 Y</param> /// <param name="sourceX">소스 X</param> /// <param name="sourceY">소스 Y</param> /// <remarks>출력 픽셀(targetX, targetY)을 다시 입력 픽셀(sourceX, sourceY)에 매핑한다.</remarks> private static void MapPixel(WarpOperation warpOperation, double middleX, double middleY, double maximumR, int targetX, int targetY, out double sourceX, out double sourceY) { const double PI_OVER_2 = Math.PI / 2.0; const double K = 100.0; const double OFFSET = -PI_OVER_2; double deltaX; double deltaY; double r1; double r2; switch(warpOperation) { case WarpOperation.Identity : sourceX = targetX; sourceY = targetY; break; case WarpOperation.FishEye : deltaX = targetX - middleX; deltaY = targetY - middleY; r1 = Math.Sqrt(deltaX * deltaX + deltaY * deltaY); if(r1 == 0) { sourceX = middleX; sourceY = middleY; } else { r2 = maximumR / 2 * (1 / (1 - r1 / maximumR) - 1); sourceX = deltaX * r2 / r1 + middleX; sourceY = deltaY * r2 / r1 + middleY; } break; case WarpOperation.Twist : deltaX = targetX - middleX; deltaY = targetY - middleY; r1 = Math.Sqrt(deltaX * deltaX + deltaY * deltaY); if(r1 == 0) { sourceX = 0; sourceY = 0; } else { double theta = Math.Atan2(deltaX, deltaY) - r1 / K - OFFSET; sourceX = r1 * Math.Cos(theta); sourceY = r1 * Math.Sin(theta); } sourceX += middleX; sourceY += middleY; break; case WarpOperation.Wave : sourceX = targetX; sourceY = targetY - 10 * (Math.Sin(targetX / 50.0 * Math.PI) + 1) + 5; break; case WarpOperation.SmallTop : deltaX = middleX - targetX; deltaX = deltaX * middleY * 2 / (targetY + 1); sourceX = middleX - deltaX; sourceY = targetY; break; case WarpOperation.Wiggles : deltaX = middleX - targetX; deltaX = deltaX * (Math.Sin(targetY / 50.0 * Math.PI) / 2 + 1.5); sourceX = middleX - deltaX; sourceY = targetY; break; case WarpOperation.DoubleWave : sourceX = targetX - 10 * (Math.Sin(targetY / 50.0 * Math.PI) + 1) + 5; sourceY = targetY - 10 * (Math.Sin(targetX / 50.0 * Math.PI) + 1) + 5; break; default : // 수직/수평 반전 sourceX = 2 * middleX - targetX; sourceY = 2 * middleY - targetY; break; } } #endregion #region 비트맵 워프하기 - WarpBitmap(sourceHelper, targetHelper, warpOperation) /// <summary> /// 비트맵 워프하기 /// </summary> /// <param name="sourceHelper">소스 비트맵 헬퍼</param> /// <param name="targetHelper">타겟 비트맵 헬퍼</param> /// <param name="warpOperation">워프 연산</param> private static void WarpBitmap(BitmapHelper sourceHelper, BitmapHelper targetHelper, WarpOperation warpOperation) { double middleX = targetHelper.Width / 2.0; double middleY = targetHelper.Height / 2.0; double maximumR = targetHelper.Width * 0.75; int maximumIndexX = sourceHelper.Width - 2; int maximumIndexY = sourceHelper.Height - 2; for(int y1 = 0; y1 < targetHelper.Height; y1++) { for (int x1 = 0; x1 < targetHelper.Width; x1++) { MapPixel(warpOperation, middleX, middleY, maximumR, x1, y1, out double x0, out double y0); int indexX0 = (int)x0; int indexY0 = (int)y0; if((indexX0 < 0) || (indexX0 > maximumIndexX) || (indexY0 < 0) || (indexY0 > maximumIndexY)) { targetHelper.SetPixel(x1, y1, 255, 255, 255, 255); } else { double deltaX0 = x0 - indexX0; double deltaY0 = y0 - indexY0; double deltaX1 = 1 - deltaX0; double deltaY1 = 1 - deltaY0; sourceHelper.GetPixel(indexX0 , indexY0 , out byte r00, out byte g00, out byte b00, out byte a00); sourceHelper.GetPixel(indexX0 , indexY0 + 1, out byte r01, out byte g01, out byte b01, out byte a01); sourceHelper.GetPixel(indexX0 + 1, indexY0 , out byte r10, out byte g10, out byte b10, out byte a10); sourceHelper.GetPixel(indexX0 + 1, indexY0 + 1, out byte r11, out byte g11, out byte b11, out byte a11); int r = (int)(r00 * deltaX1 * deltaY1 + r01 * deltaX1 * deltaY0 + r10 * deltaX0 * deltaY1 + r11 * deltaX0 * deltaY0); int g = (int)(g00 * deltaX1 * deltaY1 + g01 * deltaX1 * deltaY0 + g10 * deltaX0 * deltaY1 + g11 * deltaX0 * deltaY0); int b = (int)(b00 * deltaX1 * deltaY1 + b01 * deltaX1 * deltaY0 + b10 * deltaX0 * deltaY1 + b11 * deltaX0 * deltaY0); int a = (int)(a00 * deltaX1 * deltaY1 + a01 * deltaX1 * deltaY0 + a10 * deltaX0 * deltaY1 + a11 * deltaX0 * deltaY0); targetHelper.SetPixel(x1, y1, (byte)r, (byte)g, (byte)b, (byte)a); } } } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Public #region 비트맵 잠금 설정하기 - LockBitmap() /// <summary> /// 비트맵 잠금 설정하기 /// </summary> public void LockBitmap() { if(IsLocked) { return; } Rectangle boundRectangle = new Rectangle ( 0, 0, Bitmap.Width, Bitmap.Height ); this.bitmapData = Bitmap.LockBits ( boundRectangle, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb ); RowSizeByteCount = this.bitmapData.Stride; int totalSize = this.bitmapData.Stride * this.bitmapData.Height; ImageByteArray = new byte[totalSize]; Marshal.Copy(this.bitmapData.Scan0, ImageByteArray, 0, totalSize); this.isLocked = true; } #endregion #region 비트맵 잠금 해제하기 - UnlockBitmap() /// <summary> /// 비트맵 잠금 해제하기 /// </summary> public void UnlockBitmap() { if(!IsLocked) { return; } int totalSize = this.bitmapData.Stride * this.bitmapData.Height; Marshal.Copy(ImageByteArray, 0, this.bitmapData.Scan0, totalSize); Bitmap.UnlockBits(this.bitmapData); ImageByteArray = null; this.bitmapData = null; this.isLocked = false; } #endregion #region 픽셀 설정하기 - SetPixel(x, y, red, green, blue, alpha) /// <summary> /// 픽셀 설정하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> /// <param name="red">적색 채널</param> /// <param name="green">녹색 채널</param> /// <param name="blue">청색 채널</param> /// <param name="alpha">알파 채널</param> public void SetPixel(int x, int y, byte red, byte green, byte blue, byte alpha) { int i = y * this.bitmapData.Stride + x * 4; ImageByteArray[i++] = blue; ImageByteArray[i++] = green; ImageByteArray[i++] = red; ImageByteArray[i ] = alpha; } #endregion #region 픽셀 구하기 - GetPixel(x, y, red, green, blue, alpha) /// <summary> /// 픽셀 구하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> /// <param name="red">적색 채널</param> /// <param name="green">녹색 채널</param> /// <param name="blue">청색 채널</param> /// <param name="alpha">알파 채널</param> public void GetPixel(int x, int y, out byte red, out byte green, out byte blue, out byte alpha) { int i = y * this.bitmapData.Stride + x * 4; blue = ImageByteArray[i++]; green = ImageByteArray[i++]; red = ImageByteArray[i++]; alpha = ImageByteArray[i ]; } #endregion #region 알파 채널 설정하기 - SetAlpha(x, y, alpha) /// <summary> /// 알파 채널 설정하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> /// <param name="alpha">알파 채널</param> public void SetAlpha(int x, int y, byte alpha) { int i = y * this.bitmapData.Stride + x * 4; ImageByteArray[i + 3] = alpha; } #endregion #region 알파 채널 구하기 - GetAlpha(x, y) /// <summary> /// 알파 채널 구하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> /// <returns>알파 채널</returns> public byte GetAlpha(int x, int y) { int i = y * this.bitmapData.Stride + x * 4; return ImageByteArray[i + 3]; } #endregion #region 녹색 채널 설정하기 - SetGreen(x, y, green) /// <summary> /// 녹색 채널 설정하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> /// <param name="green">녹색 채널</param> public void SetGreen(int x, int y, byte green) { int i = y * this.bitmapData.Stride + x * 4; ImageByteArray[i + 1] = green; } #endregion #region 녹색 채널 구하기 - GetGreen(x, y) /// <summary> /// 녹색 채널 구하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> /// <returns>녹색 채널</returns> public byte GetGreen(int x, int y) { int i = y * this.bitmapData.Stride + x * 4; return ImageByteArray[i + 1]; } #endregion #region 청색 채널 설정하기 - SetBlue(x, y, blue) /// <summary> /// 청색 채널 설정하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> /// <param name="blue">청색 채널</param> public void SetBlue(int x, int y, byte blue) { int i = y * this.bitmapData.Stride + x * 4; ImageByteArray[i] = blue; } #endregion #region 청색 채널 구하기 - GetBlue(x, y) /// <summary> /// 청색 채널 구하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> /// <returns>청색 채널</returns> public byte GetBlue(int x, int y) { int i = y * this.bitmapData.Stride + x * 4; return ImageByteArray[i]; } #endregion #region 적색 채널 설정하기- SetRed(x, y, red) /// <summary> /// 적색 채널 설정하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> /// <param name="red">적색 채널</param> public void SetRed(int x, int y, byte red) { int i = y * this.bitmapData.Stride + x * 4; ImageByteArray[i + 2] = red; } #endregion #region 적색 채널 구하기 - GetRed(x, y) /// <summary> /// 적색 채널 구하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> /// <returns>적색 채널</returns> public byte GetRed(int x, int y) { int i = y * this.bitmapData.Stride + x * 4; return ImageByteArray[i + 2]; } #endregion #region 적색 채널 지우기 - ClearRed() /// <summary> /// 적색 채널 지우기 /// </summary> public void ClearRed() { bool wasLocked = IsLocked; LockBitmap(); for(int y = 0; y < Height; y++) { for(int x = 0; x < Width; x++) { SetRed(x, y, 0); } } if(!wasLocked) { UnlockBitmap(); } } #endregion #region 녹색 채널 지우기 - ClearGreen() /// <summary> /// 녹색 채널 지우기 /// </summary> public void ClearGreen() { bool wasLocked = IsLocked; LockBitmap(); for(int y = 0; y < Height; y++) { for(int x = 0; x < Width; x++) { SetGreen(x, y, 0); } } if(!wasLocked) { UnlockBitmap(); } } #endregion #region 청색 채널 지우기 - ClearBlue() /// <summary> /// 청색 채널 지우기 /// </summary> public void ClearBlue() { bool wasLocked = IsLocked; LockBitmap(); for(int y = 0; y < Height; y++) { for(int x = 0; x < Width; x++) { SetBlue(x, y, 0); } } if(!wasLocked) { UnlockBitmap(); } } #endregion #region 채널 평균 값으로 회색조 비트맵으로 변환하기 - ConvertToAverageGrayscaleBitmap() /// <summary> /// 채널 평균 값으로 회색조 비트맵으로 변환하기 /// </summary> public void ConvertToAverageGrayscaleBitmap() { bool wasLocked = IsLocked; LockBitmap(); for(int y = 0; y < Height; y++) { for(int x = 0; x < Width; x++) { GetPixel(x, y, out byte red, out byte green, out byte blue, out byte alpha); byte gray = (byte)((red + green + blue) / 3); SetPixel(x, y, gray, gray, gray, alpha); } } if(!wasLocked) { UnlockBitmap(); } } #endregion #region 회색조 비트맵으로 변환하기 - ConvertToGrayscaleBitmap() /// <summary> /// 회색조 비트맵으로 변환하기 /// </summary> public void ConvertToGrayscaleBitmap() { bool wasLocked = IsLocked; LockBitmap(); for(int y = 0; y < Height; y++) { for(int x = 0; x < Width; x++) { GetPixel(x, y, out byte red, out byte green, out byte blue, out byte alpha); byte gray = (byte)(0.3 * red + 0.5 * green + 0.2 * blue); SetPixel(x, y, gray, gray, gray, alpha); } } if(!wasLocked) { UnlockBitmap(); } } #endregion #region 비트맵 반전하기 - InvertBitmap() /// <summary> /// 비트맵 반전하기 /// </summary> public void InvertBitmap() { bool wasLocked = IsLocked; LockBitmap(); for(int y = 0; y < Height; y++) { for(int x = 0; x < Width; x++) { byte red = (byte)(255 - GetRed (x, y)); byte green = (byte)(255 - GetGreen(x, y)); byte blue = (byte)(255 - GetBlue (x, y)); byte alpha = GetAlpha(x, y); SetPixel(x, y, red, green, blue, alpha); } } if(!wasLocked) { UnlockBitmap(); } } #endregion #region 복제하기 - Clone() /// <summary> /// 복제하기 /// </summary> /// <returns>비트맵 헬퍼</returns> public BitmapHelper Clone() { bool wasLocked = this.IsLocked; LockBitmap(); BitmapHelper bitmapHelper = MemberwiseClone() as BitmapHelper; bitmapHelper.Bitmap = new Bitmap(Bitmap.Width, Bitmap.Height); bitmapHelper.isLocked = false; if(!wasLocked) { UnlockBitmap(); } return bitmapHelper; } #endregion #region 필터 적용하기 - ApplyFilter(filter, targetLocked) /// <summary> /// 필터 적용하기 /// </summary> /// <param name="filter">필터</param> /// <param name="targetLocked">타겟 잠금 여부</param> /// <returns>비트맵 헬퍼</returns> public BitmapHelper ApplyFilter(Filter filter, bool targetLocked) { BitmapHelper targetHelper = Clone(); bool wasLocked = IsLocked; LockBitmap(); targetHelper.LockBitmap(); int offsetX = -(int)(filter.KernelArray.GetUpperBound(1) / 2); int offsetY = -(int)(filter.KernelArray.GetUpperBound(0) / 2); int minimumX = -offsetX; int maximumX = Bitmap.Width - filter.KernelArray.GetUpperBound(1); int minimumY = -offsetY; int maximumY = Bitmap.Height - filter.KernelArray.GetUpperBound(0); int maximumRow = filter.KernelArray.GetUpperBound(0); int maximumColumn = filter.KernelArray.GetUpperBound(1); for(int x = minimumX; x <= maximumX; x++) { for(int y = minimumY; y <= maximumY; y++) { bool skipPixel = false; float red = 0; float green = 0; float blue = 0; for(int row = 0; row <= maximumRow; row++) { for(int column = 0; column <= maximumColumn; column++) { int indexX = x + column + offsetX; int indexY = y + row + offsetY; GetPixel(indexX, indexY, out byte newRed, out byte newGreen, out byte newBlue, out byte newAlpha); if(newAlpha == 0) { skipPixel = true; break; } red += newRed * filter.KernelArray[row, column]; green += newGreen * filter.KernelArray[row, column]; blue += newBlue * filter.KernelArray[row, column]; } if(skipPixel) { break; } } if(!skipPixel) { red = filter.Offset + red / filter.Weight; if(red < 0) { red = 0; } if(red > 255) { red = 255; } green = filter.Offset + green / filter.Weight; if(green < 0) { green = 0; } if(green > 255) { green = 255; } blue = filter.Offset + blue / filter.Weight; if(blue < 0) { blue = 0; } if(blue > 255) { blue = 255; } targetHelper.SetPixel(x, y, (byte)red, (byte)green, (byte)blue, GetAlpha(x, y)); } } } if(!targetLocked) { targetHelper.UnlockBitmap(); } if(!wasLocked) { UnlockBitmap(); } return targetHelper; } #endregion #region 워프하기 - Warp(warpOperation, targetLocked) /// <summary> /// 워프하기 /// </summary> /// <param name="warpOperation">워프 연산</param> /// <param name="targetLocked">타겟 잠금 여부</param> /// <returns>비트맵 헬퍼</returns> public BitmapHelper Warp(WarpOperation warpOperation, bool targetLocked) { BitmapHelper targetHelper = Clone(); bool wasLocked = IsLocked; LockBitmap(); targetHelper.LockBitmap(); WarpBitmap(this, targetHelper, warpOperation); if(!targetLocked) { targetHelper.UnlockBitmap(); } if(!wasLocked) { UnlockBitmap(); } return targetHelper; } #endregion } } |
▶ PointInfo.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
namespace TestProject { /// <summary> /// 포인트 정보 /// </summary> public class PointInfo { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 복소수 Z /// </summary> public Complex Z; /// <summary> /// 단계 번호 /// </summary> public int StepNumber; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - PointInfo(z, stepNumber) /// <summary> /// 생성자 /// </summary> /// <param name="z">복소수 Z</param> /// <param name="stepNumber">단계 번호</param> public PointInfo(Complex z, int stepNumber) { Z = z; StepNumber = stepNumber; } #endregion } } |
▶ Complex.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
using System; namespace TestProject { /// <summary> /// 복소수 /// </summary> public class Complex { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 실수 - RealNumber /// <summary> /// 실수 /// </summary> public double RealNumber { get; set; } #endregion #region 허수 - ImaginaryNumber /// <summary> /// 허수 /// </summary> public double ImaginaryNumber { get; set; } #endregion #region 크기 - Magnitude /// <summary> /// 크기 /// </summary> public double Magnitude { get { return Math.Sqrt(RealNumber * RealNumber + ImaginaryNumber * ImaginaryNumber); } } #endregion #region 제곱근 - SquareRoot /// <summary> /// 제곱근 /// </summary> public Complex SquareRoot { get { double newRealNumber = Math.Sqrt((Magnitude + RealNumber) / 2.0); double newImaginaryNumber = Math.Sign(ImaginaryNumber) * Math.Sqrt((Magnitude - RealNumber) / 2.0); return new Complex(newRealNumber, newImaginaryNumber); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Complex(realNumber, imaginaryNumber) /// <summary> /// 생성자 /// </summary> /// <param name="realNumber">실수</param> /// <param name="imaginaryNumber">허수</param> public Complex(double realNumber, double imaginaryNumber) { RealNumber = realNumber; ImaginaryNumber = imaginaryNumber; } #endregion #region 생성자 - Complex(realNumber) /// <summary> /// 생성자 /// </summary> /// <param name="realNumber">실수</param> public Complex(double realNumber) : this(realNumber, 0) { } #endregion #region 생성자 - Complex() /// <summary> /// 생성자 /// </summary> public Complex() : this(0, 0) { } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 연산자 + 재정의하기 - +(complex1, complex2) /// <summary> /// 연산자 + 재정의하기 /// </summary> /// <param name="complex1">복소수 1</param> /// <param name="complex2">복소수 2</param> /// <returns>복소수</returns> public static Complex operator +(Complex complex1, Complex complex2) { return new Complex(complex1.RealNumber + complex2.RealNumber, complex1.ImaginaryNumber + complex2.ImaginaryNumber); } #endregion #region 연산자 - 재정의하기 - -(complex) /// <summary> /// 연산자 - 재정의하기 /// </summary> /// <param name="complex">복소수</param> /// <returns>복소수</returns> public static Complex operator -(Complex complex) { return new Complex(-complex.RealNumber, -complex.ImaginaryNumber); } #endregion #region 연산자 - 재정의하기 - -(complex1, complex2) /// <summary> /// 연산자 - 재정의하기 /// </summary> /// <param name="complex1">복소수 1</param> /// <param name="complex2">복소수 2</param> /// <returns>복소수</returns> public static Complex operator -(Complex complex1, Complex complex2) { return complex1 + (-complex2); } #endregion #region 연산자 * 재정의하기 - *(complex1, complex2) /// <summary> /// 연산자 * 재정의하기 /// </summary> /// <param name="complex1">복소수 1</param> /// <param name="complex2">복소수 2</param> /// <returns>복소수</returns> public static Complex operator *(Complex complex1, Complex complex2) { return new Complex ( complex1.RealNumber * complex2.RealNumber - complex1.ImaginaryNumber * complex2.ImaginaryNumber, complex1.RealNumber * complex2.ImaginaryNumber + complex1.ImaginaryNumber * complex2.RealNumber ); } #endregion #region 연산자 / 재정의하기 - /(complex1, complex2) /// <summary> /// 연산자 / 재정의하기 /// </summary> /// <param name="complex1">복소수 1</param> /// <param name="complex2">복소수 2</param> /// <returns>복소수</returns> public static Complex operator /(Complex complex1, Complex complex2) { return complex1 * complex2.Inverse(); } #endregion #region 암시적 변환 정의하기 - Complex(value) /// <summary> /// 암시적 변환 정의하기 /// </summary> /// <param name="value">값</param> public static implicit operator Complex(double value) { return new Complex(value); } #endregion #region 파싱하기 - Parse(text) /// <summary> /// 파싱하기 /// </summary> /// <param name="text">텍스트</param> /// <returns>복소수</returns> public static Complex Parse(string text) { text = text.Replace(" ", ""); text = text.ToLower().Replace("i", ""); char[] signCharacterArray = { '+', '-' }; int position = text.IndexOfAny(signCharacterArray); if(position == 0) { position = text.IndexOfAny(signCharacterArray, 1); } double realNumber = double.Parse(text.Substring(0, position)); double imaginaryNumber = double.Parse(text.Substring(position)); return new Complex(realNumber, imaginaryNumber); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Public #region 극좌표 값 구하기 - GetPolarValue(r, theta) /// <summary> /// 극좌표 값 구하기 /// </summary> /// <param name="r">R</param> /// <param name="theta">세타</param> public void GetPolarValue(out double r, out double theta) { r = Math.Sqrt(RealNumber * RealNumber + ImaginaryNumber * ImaginaryNumber); theta = Math.Atan2(ImaginaryNumber, RealNumber); } #endregion #region 거듭제곱 구하기 - GetPower(power) /// <summary> /// 거듭제곱 구하기 /// </summary> /// <param name="power">거듭제곱 수</param> /// <returns>거듭제곱 값</returns> /// <remarks>De Moivre 방정식을 사용하여 숫자를 거듭제곱한다.</remarks> public Complex GetPower(double power) { GetPolarValue(out double r, out double theta); double rToPower = Math.Pow(r, power); double newRealNumber = rToPower * Math.Cos(power * theta); double newImaginaryNumber = rToPower * Math.Sin(power * theta); return new Complex(newRealNumber, newImaginaryNumber); } #endregion #region 반전하기 - Inverse() /// <summary> /// 반전하기 /// </summary> /// <returns></returns> public Complex Inverse() { double magnitude = Magnitude; return new Complex(RealNumber / magnitude, -ImaginaryNumber / magnitude); } #endregion #region 문자열 구하기 - ToString() /// <summary> /// 문자열 구하기 /// </summary> /// <returns>문자열</returns> public override string ToString() { return $"{RealNumber} + {ImaginaryNumber}i"; } #endregion } } |
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 |
using System; using System.CodeDom.Compiler; using System.Drawing; using System.Reflection; using System.Windows.Forms; using Microsoft.CSharp; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Enumeration ////////////////////////////////////////////////////////////////////////////////////////// Private #region 스무딩 타입 - SmoothingType /// <summary> /// 스무딩 타입 /// </summary> private enum SmoothingType { /// <summary> /// None /// </summary> None, /// <summary> /// Smooth1 /// </summary> Smooth1, /// <summary> /// Smooth2 /// </summary> Smooth2, } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 프랙탈 색상 배열 /// </summary> private Color[] fractalColorArray = { Color.Black, Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Cyan, Color.Blue, Color.Fuchsia }; /// <summary> /// 색상 카운트 /// </summary> private int colorCount = 0; /// <summary> /// 스무딩 타입 /// </summary> private SmoothingType smoothingType = SmoothingType.None; /// <summary> /// 최대 크기 /// </summary> private int maximumMagnitude; /// <summary> /// 최대 반복 카운트 /// </summary> private int maximumIterationCount; /// <summary> /// 로그 단위 탈출 기준 /// </summary> private double logEscape; /// <summary> /// 테스트 테두리 사각형 /// </summary> private RectangleF testBoundRectangle = RectangleF.FromLTRB(-2.2f, -2.0f, 2.2f, 2.0f); /// <summary> /// 월드 테두리 사각형 /// </summary> private RectangleF worldBoundRectangle; /// <summary> /// 시작 포인트 /// </summary> private Point startPoint; /// <summary> /// 종료 포인트 /// </summary> private Point endPoint; /// <summary> /// 영역 선택 여부 /// </summary> private bool selectingArea = false; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); Load += Form_Load; this.drawButton.Click += drawButton_Click; this.saveFileAsMenuItem.Click += saveFileAsMenuItem_Click; this.exitMenuItem.Click += exitMenuItem_Click; this.noSmoothingMenuItem.Click += noSmoothingMenuItem_Click; this.smooth1MenuItem.Click += smooth1MenuItem_Click; this.smooth2MenuItem.Click += smooth2MenuItem_Click; this.redrawMenuItem.Click += redrawMenuItem_Click; this.scaleOutX2MenuItem.Click += scaleOutX2MenuItem_Click; this.scaleOutX4MenuItem.Click += scaleOutX4MenuItem_Click; this.scaleOutX8MenuItem.Click += scaleOutX8MenuItem_Click; this.fullScaleMenuItem.Click += fullScaleMenuItem_Click; this.enterSelectedAreaMenuItem.Click += enterSelectedAreaMenuItem_Click; this.mandelbrotSetMenuItem.Click += mandelbrotSetMenuItem_Click; this.juliaSetBasicMenuItem.Click += juliaSetBasicMenuItem_Click; this.juliaSet1MenuItem.Click += juliaSet1MenuItem_Click; this.juliaSet2MenuItem.Click += juliaSet2MenuItem_Click; this.juliaSet3MenuItem.Click += juliaSet3MenuItem_Click; this.juliaSet4MenuItem.Click += juliaSet4MenuItem_Click; this.juliaSet5MenuItem.Click += juliaSet5MenuItem_Click; this.juliaSet6MenuItem.Click += juliaSet6MenuItem_Click; this.juliaSet7MenuItem.Click += juliaSet7MenuItem_Click; this.juliaSet8MenuItem.Click += juliaSet8MenuItem_Click; this.juliaSet9MenuItem.Click += juliaSet9MenuItem_Click; this.juliaSet10MenuItem.Click += juliaSet10MenuItem_Click; this.juliaSet11MenuItem.Click += juliaSet11MenuItem_Click; this.juliaSet12MenuItem.Click += juliaSet12MenuItem_Click; this.juliaishSet1MenuItem.Click += juliaishSet1MenuItem_Click; this.juliaishSet2MenuItem.Click += juliaishSet2MenuItem_Click; this.juliaishSet3MenuItem.Click += juliaishSet3MenuItem_Click; this.juliaishSet4MenuItem.Click += juliaishSet4MenuItem_Click; this.pictureBox.Paint += pictureBox_Paint; this.pictureBox.MouseDown += pictureBox_MouseDown; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Event //////////////////////////////////////////////////////////////////////////////// Private #region 폼 로드시 처리하기 - Form_Load(sender, e) /// <summary> /// 폼 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Load(object sender, EventArgs e) { this.pictureBox.Cursor = Cursors.Cross; this.colorCount = this.fractalColorArray.Length; this.maximumMagnitude = 2; this.maximumIterationCount = 64; this.logEscape = Math.Log(this.maximumMagnitude); SetWorldBoundRectangle(this.testBoundRectangle); this.pictureBox.Refresh(); } #endregion #region 다른 이름으로 저장하기 메뉴 항목 클릭시 처리하기 - saveFileAsMenuItem_Click(sender, e) /// <summary> /// 다른 이름으로 저장하기 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void saveFileAsMenuItem_Click(object sender, EventArgs e) { if(this.pictureBox.Image == null) { return; } if(this.saveFileDialog.ShowDialog() == DialogResult.OK) { this.pictureBox.Image.SaveImage(this.saveFileDialog.FileName); } } #endregion #region 종료 메뉴 항목 클릭시 처리하기 - exitMenuItem_Click(sender, e) /// <summary> /// 종료 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void exitMenuItem_Click(object sender, EventArgs e) { Close(); } #endregion #region No Smoothing 메뉴 항목 클릭시 처리하기 - noSmoothingMenuItem_Click(sender, e) /// <summary> /// No Smoothing 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void noSmoothingMenuItem_Click(object sender, EventArgs e) { SelectSmoothing(sender, SmoothingType.None); } #endregion #region Smooth 1 메뉴 항목 클릭시 처리하기 - smooth1MenuItem_Click(sender, e) /// <summary> /// Smooth 1 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void smooth1MenuItem_Click(object sender, EventArgs e) { SelectSmoothing(sender, SmoothingType.Smooth1); } #endregion #region Smooth 2 메뉴 항목 클릭시 처리하기 - smooth2MenuItem_Click(sender, e) /// <summary> /// Smooth 2 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void smooth2MenuItem_Click(object sender, EventArgs e) { SelectSmoothing(sender, SmoothingType.Smooth2); } #endregion #region 다시 그리기 메뉴 항목 클릭시 처리하기 - redrawMenuItem_Click(sender, e) /// <summary> /// 다시 그리기 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void redrawMenuItem_Click(object sender, EventArgs e) { DrawFractal(); } #endregion #region 2배 확대 메뉴 항목 클릭시 처리하기 - scaleOutX2MenuItem_Click(sender, e) /// <summary> /// 2배 확대 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void scaleOutX2MenuItem_Click(object sender, EventArgs e) { ScaleOut(2); } #endregion #region 4배 확대 메뉴 항목 클릭시 처리하기 - scaleOutX4MenuItem_Click(sender, e) /// <summary> /// 4배 확대 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void scaleOutX4MenuItem_Click(object sender, EventArgs e) { ScaleOut(4); } #endregion #region 8배 확대 메뉴 항목 클릭시 처리하기 - scaleOutX8MenuItem_Click(sender, e) /// <summary> /// 8배 확대 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void scaleOutX8MenuItem_Click(object sender, EventArgs e) { ScaleOut(8); } #endregion #region 전체 스케일 메뉴 항목 클릭시 처리하기 - fullScaleMenuItem_Click(sender, e) /// <summary> /// 전체 스케일 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void fullScaleMenuItem_Click(object sender, EventArgs e) { this.worldBoundRectangle = this.testBoundRectangle; DrawFractal(); } #endregion #region 선택 영역 입력 메뉴 항목 클릭시 처리하기 - enterSelectedAreaMenuItem_Click(sender, e) /// <summary> /// 선택 영역 입력 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void enterSelectedAreaMenuItem_Click(object sender, EventArgs e) { InputForm form = new InputForm(); form.Text = "선택 영역"; form.captionLabel.Text = "최소X, 최소Y, 최대X, 최대Y"; form.valueTextBox.Text = string.Format ( "{0}, {1}, {2}, {3}", this.worldBoundRectangle.Left, this.worldBoundRectangle.Top, this.worldBoundRectangle.Right, this.worldBoundRectangle.Bottom ); if(form.ShowDialog() == DialogResult.OK) { string[] fieldArray = form.valueTextBox.Text.Split(','); float minimumX = float.Parse(fieldArray[0]); float minimumY = float.Parse(fieldArray[1]); float maximumX = float.Parse(fieldArray[2]); float maximumY = float.Parse(fieldArray[3]); this.worldBoundRectangle = new RectangleF ( minimumX, minimumY, maximumX - minimumX, maximumY - minimumY ); DrawFractal(); } } #endregion #region 픽처 박스 페인트시 처리하기 - pictureBox_Paint(sender, e) /// <summary> /// 픽처 박스 페인트시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void pictureBox_Paint(object sender, PaintEventArgs e) { if(this.selectingArea) { e.Graphics.DrawDashedBox(Color.Red, Color.Yellow, 2, 5, this.startPoint, this.endPoint); } } #endregion #region 픽처 박스 마우스 DOWN 처리하기 - pictureBox_MouseDown(sender, e) /// <summary> /// 픽처 박스 마우스 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void pictureBox_MouseDown(object sender, MouseEventArgs e) { this.startPoint = e.Location; this.endPoint = e.Location; this.pictureBox.MouseMove += pictureBox_MouseMove; this.pictureBox.MouseUp += pictureBox_MouseUp; this.selectingArea = true; this.pictureBox.Refresh(); } #endregion #region 픽처 박스 마우스 이동시 처리하기 - pictureBox_MouseMove(sender, e) /// <summary> /// 픽처 박스 마우스 이동시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void pictureBox_MouseMove(object sender, MouseEventArgs e) { this.endPoint = e.Location; this.pictureBox.Refresh(); } #endregion #region 픽처 박스 마우스 UP 처리하기 - pictureBox_MouseUp(sender, e) /// <summary> /// 픽처 박스 마우스 UP 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void pictureBox_MouseUp(object sender, MouseEventArgs e) { this.selectingArea = false; this.pictureBox.MouseMove -= pictureBox_MouseMove; this.pictureBox.MouseUp -= pictureBox_MouseUp; PointF startWorldPoint = GetWorldPoint(this.startPoint); PointF endWorldPoint = GetWorldPoint(this.endPoint ); SetWorldBoundRectangle(startWorldPoint.ToRectangle(endWorldPoint)); } #endregion #region Mandelbrot Set 메뉴 항목 클릭시 처리하기 - mandelbrotSetMenuItem_Click(sender, e) /// <summary> /// Mandelbrot Set 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void mandelbrotSetMenuItem_Click(object sender, EventArgs e) { this.z0TextBox.Text = "0"; this.znPlus1TextBox.Text = "Zn.GetPower(2) + x + y * i"; DrawFractal(); } #endregion #region Julia Set Basic 메뉴 항목 클릭시 처리하기 - juliaSetBasicMenuItem_Click(sender, e) /// <summary> /// Julia Set Basic 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void juliaSetBasicMenuItem_Click(object sender, EventArgs e) { this.z0TextBox.Text = "x + y * i"; this.znPlus1TextBox.Text = "Zn.GetPower(2) - i"; DrawFractal(); } #endregion #region Julia Set 1 메뉴 항목 클릭시 처리하기 - juliaSet1MenuItem_Click(sender, e) /// <summary> /// Julia Set 1 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void juliaSet1MenuItem_Click(object sender, EventArgs e) { this.z0TextBox.Text = "x + y * i"; this.znPlus1TextBox.Text = "Zn.GetPower(2) + (-0.4 + 0.6 * i)"; DrawFractal(); } #endregion #region Julia Set 2 메뉴 항목 클릭시 처리하기 - juliaSet2MenuItem_Click(sender, e) /// <summary> /// Julia Set 2 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void juliaSet2MenuItem_Click(object sender, EventArgs e) { this.z0TextBox.Text = "x + y * i"; this.znPlus1TextBox.Text = "Zn.GetPower(2) + (0.285 + 0 * i)"; DrawFractal(); } #endregion #region Julia Set 3 메뉴 항목 클릭시 처리하기 - juliaSet3MenuItem_Click(sender, e) /// <summary> /// Julia Set 3 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void juliaSet3MenuItem_Click(object sender, EventArgs e) { this.z0TextBox.Text = "x + y * i"; this.znPlus1TextBox.Text = "Zn.GetPower(2) + (0.285 + 0.01 * i)"; DrawFractal(); } #endregion #region Julia Set 4 메뉴 항목 클릭시 처리하기 - juliaSet4MenuItem_Click(sender, e) /// <summary> /// Julia Set 4 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void juliaSet4MenuItem_Click(object sender, EventArgs e) { this.z0TextBox.Text = "x + y * i"; this.znPlus1TextBox.Text = "Zn.GetPower(2) + (0.45 + 0.1428 * i)"; DrawFractal(); } #endregion #region Julia Set 5 메뉴 항목 클릭시 처리하기 - juliaSet5MenuItem_Click(sender, e) /// <summary> /// Julia Set 5 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void juliaSet5MenuItem_Click(object sender, EventArgs e) { this.z0TextBox.Text = "x + y * i"; this.znPlus1TextBox.Text = "Zn.GetPower(2) + (-0.70176 - 0.3842 * i)"; DrawFractal(); } #endregion #region Julia Set 6 메뉴 항목 클릭시 처리하기 - juliaSet6MenuItem_Click(sender, e) /// <summary> /// Julia Set 6 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void juliaSet6MenuItem_Click(object sender, EventArgs e) { this.z0TextBox.Text = "x + y * i"; this.znPlus1TextBox.Text = "Zn.GetPower(2) + (-0.835 - 0.2321 * i)"; DrawFractal(); } #endregion #region Julia Set 7 메뉴 항목 클릭시 처리하기 - juliaSet7MenuItem_Click(sender, e) /// <summary> /// Julia Set 7 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void juliaSet7MenuItem_Click(object sender, EventArgs e) { this.z0TextBox.Text = "x + y * i"; this.znPlus1TextBox.Text = "Zn.GetPower(2) + (-0.8 + 0.156 * i)"; DrawFractal(); } #endregion #region Julia Set 8 메뉴 항목 클릭시 처리하기 - juliaSet8MenuItem_Click(sender, e) /// <summary> /// Julia Set 8 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void juliaSet8MenuItem_Click(object sender, EventArgs e) { this.z0TextBox.Text = "x + y * i"; this.znPlus1TextBox.Text = "Zn.GetPower(2) + (-0.7269 + 0.1889 * i)"; DrawFractal(); } #endregion #region Julia Set 9 메뉴 항목 클릭시 처리하기 - juliaSet9MenuItem_Click(sender, e) /// <summary> /// Julia Set 9 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void juliaSet9MenuItem_Click(object sender, EventArgs e) { this.z0TextBox.Text = "x + y * i"; this.znPlus1TextBox.Text = "Zn.GetPower(2) + (0.0 - 0.8 * i)"; DrawFractal(); } #endregion #region Julia Set 10 메뉴 항목 클릭시 처리하기 - juliaSet10MenuItem_Click(sender, e) /// <summary> /// Julia Set 10 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void juliaSet10MenuItem_Click(object sender, EventArgs e) { this.z0TextBox.Text = "x + y * i"; this.znPlus1TextBox.Text = "Zn.GetPower(2) + (-0.74543 + 0.11301 * i)"; DrawFractal(); } #endregion #region Julia Set 11 메뉴 항목 클릭시 처리하기 - juliaSet11MenuItem_Click(sender, e) /// <summary> /// Julia Set 11 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void juliaSet11MenuItem_Click(object sender, EventArgs e) { this.z0TextBox.Text = "x + y * i"; this.znPlus1TextBox.Text = "Zn.GetPower(2) + (-0.75 + 0.11 * i)"; DrawFractal(); } #endregion #region Julia Set 12 메뉴 항목 클릭시 처리하기 - juliaSet12MenuItem_Click(sender, e) /// <summary> /// Julia Set 12 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void juliaSet12MenuItem_Click(object sender, EventArgs e) { this.z0TextBox.Text = "x + y * i"; this.znPlus1TextBox.Text = "Zn.GetPower(2) + (-0.1 + 0.651 * i)"; DrawFractal(); } #endregion #region Juliaish Set 1 메뉴 항목 클릭시 처리하기 - juliaishSet1MenuItem_Click(sender, e) /// <summary> /// Juliaish Set 1 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void juliaishSet1MenuItem_Click(object sender, EventArgs e) { this.z0TextBox.Text = "x + y * i"; this.znPlus1TextBox.Text = "(1 - Zn.GetPower(3) / 6) / (Zn - Zn.GetPower(2) / 2).GetPower(2) + 0.15"; DrawFractal(); } #endregion #region Juliaish Set 2 메뉴 항목 클릭시 처리하기 - juliaishSet2MenuItem_Click(sender, e) /// <summary> /// Juliaish Set 2 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void juliaishSet2MenuItem_Click(object sender, EventArgs e) { this.z0TextBox.Text = "x + y * i"; this.znPlus1TextBox.Text = "1 - Zn.GetPower(2) + Zn.GetPower(5) / (2 + 4 * Zn)"; DrawFractal(); } #endregion #region Juliaish Set 3 메뉴 항목 클릭시 처리하기 - juliaishSet3MenuItem_Click(sender, e) /// <summary> /// Juliaish Set 3 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void juliaishSet3MenuItem_Click(object sender, EventArgs e) { this.z0TextBox.Text = "x + y * i"; this.znPlus1TextBox.Text = "(1 - Zn.GetPower(2) + Zn.GetPower(5)) / (2 + 4 * Zn)"; DrawFractal(); } #endregion #region Juliaish Set 4 메뉴 항목 클릭시 처리하기 - juliaishSet4MenuItem_Click(sender, e) /// <summary> /// Juliaish Set 4 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void juliaishSet4MenuItem_Click(object sender, EventArgs e) { this.z0TextBox.Text = "x + y * i"; this.znPlus1TextBox.Text = "1 - Zn.GetPower(2) + Zn.GetPower(5) / (2 + 4 * Zn) + 0.2"; DrawFractal(); } #endregion #region 그리기 버튼 클릭시 처리하기 - drawButton_Click(sender, e) /// <summary> /// 그리기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void drawButton_Click(object sender, EventArgs e) { DrawFractal(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Function //////////////////////////////////////////////////////////////////////////////// Private #region 테두리 사각형 조정하기 - AdjustBoundRectangle() /// <summary> /// 테두리 사각형 조정하기 /// </summary> private void AdjustBoundRectangle() { float worldWidth = this.worldBoundRectangle.Width; float worldHeight = this.worldBoundRectangle.Height; float worldAspectRatio = worldWidth / worldHeight; float deviceAspectRation = (float)this.pictureBox.ClientSize.Width / (float)this.pictureBox.ClientSize.Height; if(worldAspectRatio > deviceAspectRation) { worldHeight = worldWidth / deviceAspectRation; } else { worldWidth = worldHeight * deviceAspectRation; } float centerX = this.worldBoundRectangle.X + this.worldBoundRectangle.Width / 2f; float centerY = this.worldBoundRectangle.Y + this.worldBoundRectangle.Height / 2f; this.worldBoundRectangle = new RectangleF ( centerX - worldWidth / 2f, centerY - worldHeight / 2f, worldWidth, worldHeight ); } #endregion #region 월드 테두리 사각형 설정하기 - SetWorldBoundRectangle(boundRectangle) /// <summary> /// 월드 테두리 사각형 설정하기 /// </summary> /// <param name="boundRectangle">테두리 사각형</param> private void SetWorldBoundRectangle(RectangleF boundRectangle) { this.worldBoundRectangle = boundRectangle; AdjustBoundRectangle(); DrawFractal(); } #endregion #region 장치 포인트 구하기 - GetDevicePoint(worldPoint) /// <summary> /// 장치 포인트 구하기 /// </summary> /// <param name="worldPoint">월드 포인트</param> /// <returns>장치 포인트</returns> private PointF GetDevicePoint(PointF worldPoint) { float scaleX = (worldPoint.X - this.worldBoundRectangle.X) / this.worldBoundRectangle.Width; float scaleY = (worldPoint.Y - this.worldBoundRectangle.Y) / this.worldBoundRectangle.Height; float x = 0 + scaleX * this.pictureBox.ClientSize.Width; float y = 0 + scaleY * this.pictureBox.ClientSize.Height; return new PointF(x, y); } #endregion #region 월드 포인트 구하기 - GetWorldPoint(devicePoint) /// <summary> /// 월드 포인트 구하기 /// </summary> /// <param name="devicePoint">장치 포인트</param> /// <returns>월드 포인트</returns> private PointF GetWorldPoint(PointF devicePoint) { float scaleX = (devicePoint.X - 0) / this.pictureBox.ClientSize.Width; float scaleY = (devicePoint.Y - 0) / this.pictureBox.ClientSize.Height; float x = this.worldBoundRectangle.X + scaleX * this.worldBoundRectangle.Width; float y = this.worldBoundRectangle.Y + scaleY * this.worldBoundRectangle.Height; return new PointF(x, y); } #endregion #region 메뉴 항목 선택하기 - SelectMenuItem(sender, menuItemArray) /// <summary> /// 메뉴 항목 선택하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="menuItemArray">메뉴 항목 배열</param> private void SelectMenuItem(object sender, ToolStripMenuItem[] menuItemArray) { ToolStripMenuItem selectedMenuItem = sender as ToolStripMenuItem; foreach(ToolStripMenuItem menuItem in menuItemArray) { menuItem.Checked = (menuItem == selectedMenuItem); } } #endregion #region 스무딩 선택하기 - SelectSmoothing(sender, smoothingType) /// <summary> /// 스무딩 선택하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="smoothingType">스무딩 타입</param> private void SelectSmoothing(object sender, SmoothingType smoothingType) { ToolStripMenuItem[] menuItemArray = { this.noSmoothingMenuItem, this.smooth1MenuItem, this.smooth2MenuItem, }; SelectMenuItem(sender, menuItemArray); this.smoothingType = smoothingType; DrawFractal(); } #endregion #region 확대하기 - ScaleOut(scale) /// <summary> /// 확대하기 /// </summary> /// <param name="scale">스케일</param> private void ScaleOut(float scale) { float centerX = this.worldBoundRectangle.X + this.worldBoundRectangle.Width / 2f; float centerY = this.worldBoundRectangle.Y + this.worldBoundRectangle.Height / 2f; float width = this.worldBoundRectangle.Width * scale; float height = this.worldBoundRectangle.Height * scale; this.worldBoundRectangle = new RectangleF ( centerX - width / 2, centerY - height / 2, width, height ); DrawFractal(); } #endregion #region 메소드 정보 구하기 - GetMethodInfo(maximumStep, maximumMagnitude, z0, zn1) /// <summary> /// 메소드 정보 구하기 /// </summary> /// <param name="maximumStep">최대 단계</param> /// <param name="maximumMagnitude">최대 크기</param> /// <param name="z0">Z0</param> /// <param name="znPlus1">Zn + 1</param> /// <returns>메소드 정보</returns> private MethodInfo GetMethodInfo(string maximumStep, string maximumMagnitude, string z0, string znPlus1) { string functionText = @" using TestProject; public static class Evaluator { public static PointInfo EvaluatePoint(float x, float y) { Complex i = new Complex(0, 1); Complex Zn = " + z0 + @"; int step_num; for (step_num = 0; step_num < " + maximumStep + @"; step_num++) { if (Zn.Magnitude > " + maximumMagnitude + @") break; // Calculate Zn+1. Complex ZnPlus1 = " + znPlus1 + @"; Zn = ZnPlus1; } return new PointInfo(Zn, step_num); } };"; CompilerParameters parameters = new CompilerParameters(); parameters.GenerateInMemory = true; parameters.GenerateExecutable = false; parameters.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location); CSharpCodeProvider codeProvider = new CSharpCodeProvider(); CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, functionText); if(results.Errors.Count > 0) { string message = "Error compiling the function."; foreach(CompilerError compilerError in results.Errors) { message += "\n" + compilerError.ErrorText; } MessageBox.Show(message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return null; } else { Type evaluatorType = results.CompiledAssembly.GetType("Evaluator"); MethodInfo methodInfo = evaluatorType.GetMethod("EvaluatePoint"); return methodInfo; } } #endregion #region 색상 구하기 - GetColor(mu) /// <summary> /// 색상 구하기 /// </summary> /// <param name="mu">MU</param> /// <returns>색상</returns> private Color GetColor(double mu) { int color1 = (int)mu; double t2 = mu - color1; double t1 = 1 - t2; color1 = color1 % this.colorCount; int color2 = (color1 + 1) % this.colorCount; double red = this.fractalColorArray[color1].R * t1 + this.fractalColorArray[color2].R * t2; double green = this.fractalColorArray[color1].G * t1 + this.fractalColorArray[color2].G * t2; double blue = this.fractalColorArray[color1].B * t1 + this.fractalColorArray[color2].B * t2; return Color.FromArgb((byte)red, (byte)green, (byte)blue); } #endregion #region 스무스 망델브로 색상 구하기 - GetSmoothMandelbrotColor(z, c, stepNumber) /// <summary> /// 스무스 망델브로 색상 구하기 /// </summary> /// <param name="z">복소수 Z</param> /// <param name="c">복소수 C</param> /// <param name="stepNumber">단계 번호</param> /// <returns>스무스 망델브로 색상</returns> private Color GetSmoothMandelbrotColor(Complex z, Complex c, int stepNumber) { if(stepNumber == 0) { return this.fractalColorArray[0]; } for(int i = 0; i < 3; i++) { z = z * z + c; stepNumber++; } double mu = stepNumber + 1 - Math.Log(Math.Log(z.Magnitude)) / this.logEscape; if(this.smoothingType == SmoothingType.Smooth2) { mu = mu / this.maximumIterationCount * this.colorCount; } return GetColor(mu); } #endregion #region 픽셀 색상 설정하기 - SetPixelColor(bitmapHelper, indexX, indexY, methodInfo, z, stepNumber) /// <summary> /// 픽셀 색상 설정하기 /// </summary> /// <param name="bitmapHelper">비트맵 헬퍼</param> /// <param name="indexX">인덱스 X</param> /// <param name="indexY">인덱스 Y</param> /// <param name="methodInfo">메소드 정보</param> /// <param name="z">복소수 Z</param> /// <param name="stepNumber">단계 번호</param> private void SetPixelColor(BitmapHelper bitmapHelper, int indexX, int indexY, MethodInfo methodInfo, Complex z, int stepNumber) { if(stepNumber >= this.maximumIterationCount) { stepNumber = 0; } Color color; if(this.smoothingType == SmoothingType.None) { color = this.fractalColorArray[stepNumber % this.colorCount]; } else { color = GetSmoothMandelbrotColor(z, new Complex(), stepNumber); } bitmapHelper.SetPixel(indexX, indexY, color.R, color.G, color.B, 255); } #endregion #region 프랙탈 그리기 - DrawFractal(minimumX, minimumY, deltaX, deltaY, width, height) /// <summary> /// 프랙탈 그리기 /// </summary> /// <param name="minimumX">최소 X</param> /// <param name="minimumY">최소 Y</param> /// <param name="deltaX">델타 X</param> /// <param name="deltaY">델타 Y</param> /// <param name="width">너비</param> /// <param name="height">높이</param> /// <returns>비트맵</returns> private Bitmap DrawFractal(float minimumX, float minimumY, float deltaX, float deltaY, int width, int height) { MethodInfo methodInfo = GetMethodInfo ( this.maximumStepTextBox.Text, this.maximumMagnitudeTextBox.Text, this.z0TextBox.Text, this.znPlus1TextBox.Text ); if(methodInfo == null) { return null; } Bitmap bitmap = new Bitmap(width, height); BitmapHelper bitmapHelper = new BitmapHelper(bitmap); bitmapHelper.LockBitmap(); object[] methodParameterArray = new object[2]; float y = minimumY; for(int indexY = 0; indexY < bitmapHelper.Height; indexY++) { float x = minimumX; for(int indexX = 0; indexX < bitmapHelper.Width; indexX++) { methodParameterArray[0] = x; methodParameterArray[1] = y; PointInfo pointInfo = (PointInfo)methodInfo.Invoke(null, methodParameterArray); SetPixelColor(bitmapHelper, indexX, indexY, methodInfo, pointInfo.Z, pointInfo.StepNumber); x += deltaX; } y += deltaY; } bitmapHelper.UnlockBitmap(); return bitmap; } #endregion #region 프랙탈 그리기 - DrawFractal() /// <summary> /// 프랙탈 그리기 /// </summary> private void DrawFractal() { this.pictureBox.Image = null; this.pictureBox.Refresh(); Cursor = Cursors.WaitCursor; AdjustBoundRectangle(); float deltaX = this.worldBoundRectangle.Width / this.pictureBox.ClientRectangle.Width; float deltaY = this.worldBoundRectangle.Height / this.pictureBox.ClientRectangle.Height; float minimumX = this.worldBoundRectangle.X; float minimumY = this.worldBoundRectangle.Y; string z0 = this.z0TextBox.Text; string equation = this.znPlus1TextBox.Text; int width = this.pictureBox.ClientSize.Width; int height = this.pictureBox.ClientSize.Height; Bitmap bitmap = DrawFractal(minimumX, minimumY, deltaX, deltaY, width, height); this.pictureBox.Image = bitmap; Cursor = Cursors.Default; } #endregion } } |