Microsoft MVP성태의 닷넷 이야기
Math: 7. C# - 펜타그램(Pentagram) 그리기 [링크 복사], [링크+제목 복사],
조회: 32603
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
(연관된 글이 3개 있습니다.)

C# - 펜타그램(Pentagram) 그리기

요즘 제가 ^^ "황금 비율의 진실"에 관한 책을 읽고 있는데요.

황금 비율의 진실: 완벽을 창조하는 가장 아름다운 비율의 미스터리와 허구 
; http://www.yes24.com/24/goods/5266967?scode=032

개인적으로 너무 너무 재미있게 읽고 있습니다. ^^

읽어보다가, 호기심이 발동해서 펜타그램을 C# 코드로 그려보고 싶어졌는데요. 펜타그램을 구하기 위해서 원에서 같은 간격으로 5개의 지점에 꼭지점을 선택해야 하는데, 360 / 5 = 72도로 해서 분할을 할 수가 있습니다. 말보다는, 다음의 글에 실린 그림만 보시면 직관적으로 알 수 있겠지요.

How to Draw a Perfect Pentagram
; http://www.wikihow.com/Draw-a-Perfect-Pentagram

pentagram_drawing_1.jpg

그럼, 실제로 점에 대한 좌표는 어떻게 구할 수 있을까요? 그렇습니다. ^^ 삼각함수를 이용하면 됩니다. 가령 원의 반지름이 200 이라고 가정하면,

pentagram_drawing_2.png

18도를 삼각함수로 이용해서 x, y 좌표를 다음과 같이 구할 수 있습니다.

A의 x 좌표: cos(18도) = x / 200
            x = cos(18도) * 200
            x = 190.2113032590307...

A의 y 좌표: sin(18도) = y / 200
            y = sin(18도) * 200
            y = 61.803398874989476...

.NET Framework 의 Math 라이브러리는 각도가 아니라 라디안 값을 인자로 받기 때문에 단위 변경을 해주는 것을 잊지 말아야 합니다.

C# Convert Radians and Degrees
; http://www.vcskicks.com/csharp_net_angles.php

private double RadianToDegree(double angle)
{
    return angle * (180.0 / Math.PI);
}

double DegreeToRadian(double angle)
{
    return Math.PI * angle / 180.0;
}

그렇게 해서 72도씩 진행해 나가면 5개의 꼭지점 위치를 결정할 수 있고,

pentagram_drawing_3.png

이제 꼭지점마다 가로질러 선을 그어주면 펜타그램이 완성됩니다. ^^

pentagram_drawing_4.png

그런데, 소스가 좀 지저분 한 듯 싶어서 검색을 해보았더니 ^^ 더 좋은 알고리즘이 있습니다.

Thread: How to calculate points of a Pentagon? 
; http://forums.codeguru.com/showthread.php?511526-How-to-calculate-points-of-a-Pentagon

private void DrawPentagram(Graphics g)
{
    Point [] pts = new Point[6];

    int centerX = this.Width / 2;
    int centerY = this.Height / 2;
    int radius = 100;

    Point location = new Point(centerX, centerY);

    for (int i = 0; i < 6; i++)
    {
        double radian = (0.8 * Math.PI * i) + (0.7 * Math.PI);
        pts[i] = location + new Size((int)(radius * Math.Cos(radian)),
            (int)(radius * Math.Sin(radian)));
    }

    for (int i = 0; i < 5; i ++)
    {
        g.DrawLine(Pens.Red, pts[0], pts[1]);
    }
}

똑바로 선 펜타그램을 그리기 위해서 약간 소스 코드를 변경하긴 했지만, 어쨌든 이렇게 현재 점으로부터 144도에 위치한 점을 바로 찾아내서 선을 긋기 때문에 제가 만든 소스 코드보다 훨씬 더 간결하게 나왔습니다.

pentagram_drawing_5.png

소스 코드는 2개 모두 첨부해 두었습니다. ^^




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 7/13/2012]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 76  77  78  79  80  81  [82]  83  84  85  86  87  88  89  90  ...
NoWriterDateCnt.TitleFile(s)
11887정성태5/8/201921477.NET Framework: 829. C# - yield 문을 사용할 수 있는 메서드의 조건
11886정성태5/7/201919265오류 유형: 534. mstest.exe 실행 시 "Visual Studio Enterprise is required to execute the test." 오류 [2]
11885정성태5/7/201916214오류 유형: 533. mstest.exe 실행 시 "File extension specified '.loadtest' is not a valid test extension." 오류 발생
11884정성태5/5/201921006.NET Framework: 828. C# DLL에서 Win32 C/C++처럼 dllexport 함수를 제공하는 방법 - 두 번째 이야기
11883정성태5/3/201926212.NET Framework: 827. C# - 인터넷 시간 서버로부터 받은 시간을 윈도우에 적용하는 방법파일 다운로드1
11882정성태5/2/201922485.NET Framework: 826. (번역글) .NET Internals Cookbook Part 11 - Various C# riddles파일 다운로드1
11881정성태4/28/201922612오류 유형: 532. .NET Core 프로젝트로 마이그레이션 시 "CS0579 Duplicate 'System.Reflection.AssemblyCompanyAttribute' attribute" 오류 발생
11880정성태4/25/201918454오류 유형: 531. 이벤트 로그 오류 - Task Scheduling Error: m->NextScheduledSPRetry 1547, m->NextScheduledEvent 1547
11879정성태4/24/201926852.NET Framework: 825. (번역글) .NET Internals Cookbook Part 10 - Threads, Tasks, asynchronous code and others파일 다운로드2
11878정성태4/22/201922597.NET Framework: 824. (번역글) .NET Internals Cookbook Part 9 - Finalizers, queues, card tables and other GC stuff파일 다운로드1
11877정성태4/22/201922694.NET Framework: 823. (번역글) .NET Internals Cookbook Part 8 - C# gotchas파일 다운로드1
11876정성태4/21/201921728.NET Framework: 822. (번역글) .NET Internals Cookbook Part 7 - Word tearing, locking and others파일 다운로드1
11875정성태4/21/201922742오류 유형: 530. Visual Studo에서 .NET Core 프로젝트를 열 때 "One or more errors occurred." 오류 발생
11874정성태4/20/201922933.NET Framework: 821. (번역글) .NET Internals Cookbook Part 6 - Object internals파일 다운로드1
11873정성태4/19/201921408.NET Framework: 820. (번역글) .NET Internals Cookbook Part 5 - Methods, parameters, modifiers파일 다운로드1
11872정성태4/17/201922290.NET Framework: 819. (번역글) .NET Internals Cookbook Part 4 - Type members파일 다운로드1
11871정성태4/16/201920928.NET Framework: 818. (번역글) .NET Internals Cookbook Part 3 - Initialization tricks [3]파일 다운로드1
11870정성태4/16/201919201.NET Framework: 817. Process.Start로 실행한 콘솔 프로그램의 출력 결과를 얻는 방법파일 다운로드1
11869정성태4/15/201925026.NET Framework: 816. (번역글) .NET Internals Cookbook Part 2 - GC-related things [2]파일 다운로드2
11868정성태4/15/201921024.NET Framework: 815. CER(Constrained Execution Region)이란?파일 다운로드1
11867정성태4/15/201920163.NET Framework: 814. Critical Finalizer와 SafeHandle의 사용 의미파일 다운로드1
11866정성태4/9/201923313Windows: 159. 네트워크 공유 폴더(net use)에 대한 인증 정보는 언제까지 유효할까요?
11865정성태4/9/201919047오류 유형: 529. 제어판 - C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools is not accessible.
11864정성태4/9/201917820오류 유형: 528. '...' could be '0': this does not adhere to the specification for the function '...'
11863정성태4/9/201917690디버깅 기술: 127. windbg - .NET x64 EXE의 EntryPoint
11862정성태4/7/201920162개발 환경 구성: 437. .NET EXE의 ASLR 기능을 끄는 방법
... 76  77  78  79  80  81  [82]  83  84  85  86  87  88  89  90  ...