C#
원그리기
안녕1999
2020. 12. 5. 23:44
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; // Add reference for VISA-COM 5.9 Type Library using Ivi.Visa.Interop; using System.Diagnostics; using System.Runtime.InteropServices; //using Math.Util; using System.Drawing; using System.Drawing.Imaging; using System.IO; public struct POINT { public int x, y; } public class Test { /* double 라디안 = 각도 * (float)(Math.PI / 180); double x2 = Math.Cos(라디안) * x1 - Math.Sin(라디안) * y1; double y2 = Math.Sin(라디안) * x1 + Math.Cos(라디안) * y1; */ [DllImport("user32.dll")] public static extern bool GetCursorPos(out POINT lpPoint); static int CirclePoint_n = 360 * 10; static double[] CirclePoint_x = new double[CirclePoint_n]; static double[] CirclePoint_y = new double[CirclePoint_n]; static bool bCreateCirclePoint; static void CreateCirclePoint() { int i; double ra = 0, x0, y0; x0 = 1; y0 = 0; for (i = 0; i < CirclePoint_n; i++) { double c, s; //x1=x*cos()-y*sin(); //y1=x*sin()+y*cos(); ra = Math.PI * 2 * i / CirclePoint_n; c = Math.Cos(ra); s = Math.Sin(ra); CirclePoint_x[i] = x0 * c - y0 * s; CirclePoint_y[i] = x0 * s + y0 * c; //G.DEBUG_puts(i.ToString()+"\t" + CirclePoint_x[i] + "\t" + CirclePoint_y[i]); } bCreateCirclePoint = true; //RotateMoveScale(x, y, n, ref x, ref y, 45,400,200,500); } //DrawPolygon(Pen, PointF[]) //반지름이 1인 원의 좌표중 m개의 점만 얻는다 //원의 좌표들을 미리 계산하여, RotateMoveScale하여 사용한다 //7각형같은 다각형의 좌표로도 활용가능 static void GetCirclePoint(ref double[] x, ref double[] y, int m) { int i, idx = 0, inc = CirclePoint_n / m; for (i = 0; i < m; i++) { x[i] = CirclePoint_x[idx]; y[i] = CirclePoint_y[idx]; idx += inc; } } static void RotateMoveScale(double[] x, double[] y, int n, ref double[] x2, ref double[] y2, double deg = 0,//deg=각도 0~360 double dx = 0, double dy = 0, double Scale = 1) { int i; double ra, ix, iy; if (deg == 0) { if ((dx == 0) && (dy == 0)) { if (Scale == 1) { for (i = 0; i < n; i++) { x2[i] = x[i]; y2[i] = y[i]; } } else { for (i = 0; i < n; i++) { x2[i] = x[i] * Scale; y2[i] = y[i] * Scale; } } } else { if (Scale == 1) { for (i = 0; i < n; i++) { x2[i] = x[i] + dx; y2[i] = y[i] + dy; } } else { for (i = 0; i < n; i++) { x2[i] = x[i] * Scale + dx; y2[i] = y[i] * Scale + dy; } } } } else { double c, s; ra = deg * (Math.PI / 180); c = Math.Cos(ra); s = Math.Sin(ra); if ((dx == 0) && (dy == 0)) { if (Scale == 1) { for (i = 0; i < n; i++) { ix = x[i];//x와 x2가 같은 경우 문제가 됨 iy = y[i]; x2[i] = ix * c - iy * s; y2[i] = ix * s + iy * c; } } else { for (i = 0; i < n; i++) { ix = x[i];//x와 x2가 같은 경우 문제가 됨 iy = y[i]; x2[i] = (ix * c - iy * s) * Scale; y2[i] = (ix * s + iy * c) * Scale; } } } else { if (Scale == 1) { for (i = 0; i < n; i++) { ix = x[i];//x와 x2가 같은 경우 문제가 됨 iy = y[i]; x2[i] = ix * c - iy * s + dx; y2[i] = ix * s + iy * c + dy; } } else { for (i = 0; i < n; i++) { ix = x[i];//x와 x2가 같은 경우 문제가 됨 iy = y[i]; x2[i] = (ix * c - iy * s) * Scale + dx; y2[i] = (ix * s + iy * c) * Scale + dy; } } } } } static void DrawPolygon(Graphics g, double[] x, double[] y, int n) { PointF[] p = new PointF[n]; int i; for (i = 0; i < n; i++) { p[i] = new PointF((float)x[i], (float)y[i]); } g.DrawPolygon(System.Drawing.Pens.LightGray, p); } static void Move(double[] x, double[] y, int n, ref double[] x2, ref double[] y2, double dx, double dy) { int i; for (i = 0; i < n; i++) { x2[i] = x[i] + dx; y2[i] = y[i] + dy; } } void Rotate(double[] x, double[] y, int n, double deg,//deg=각도 0~360 ref double[] x2, ref double[] y2) { int i; double ra = deg * (Math.PI / 180), c, s; c = Math.Cos(ra); s = Math.Sin(ra); for (i = 0; i < n; i++) { x2[i] = x[i] * c - y[i] * s; y2[i] = x[i] * s + y[i] * c; } } //시작점,중심점으로 각도 계산 static public double Get_deg_from_two_point(double ox, double oy, double x, double y) { return (180 / Math.PI * Math.Atan2(y - oy, x - ox)); } static void test5() { double d; d = Get_deg_from_two_point(0, 0, 1, 0);//d=0 G.DEBUG_puts_double("d", d); d = Get_deg_from_two_point(0, 0, 1, 1);//d=45 G.DEBUG_puts_double("d", d); d = Get_deg_from_two_point(0, 0, 0, 1);//d=90 G.DEBUG_puts_double("d", d); d = Get_deg_from_two_point(0, 0, -1, 1);//d=135 G.DEBUG_puts_double("d", d); d = Get_deg_from_two_point(0, 0, -1, 0);//d=180 G.DEBUG_puts_double("d", d); d = Get_deg_from_two_point(0, 0, -1, -1);//d=-135 G.DEBUG_puts_double("d", d); d = Get_deg_from_two_point(0, 0, 1, -1);//d=-45 G.DEBUG_puts_double("d", d); } //두점을 연결하는 직선의 방정식 //y = ((y2 ? y1) / (x2 ? x1)) (x ? x1) + y1 //두께가 d이고, 2점을 연결하는 직선1과, px,py에서 시작하는 수평선이 직선1과 만나는 지점구하기 //(직선1과는 d/2만큼 떨어진 좌표이다.) //수평선은 직선1의 방정식에서 y값을 알고 있고, x좌표만 구하면 된다.(-d/2) static void Circle_r(Graphics g, double x1, double y1, double r) { } static void Circle_d(Graphics g, double x1, double y1, double d) { Circle_r(g, x1, y1, d / 2); } //두께가 d인 라인 그리기 static void DrawWidthLine(Graphics g, double x1, double y1, double x2, double y2, double d) { Circle_d(g, x1, y1, d); Circle_d(g, x2, y2, d); } static public double angle; static public void DEBUG_Test_Paint(Form wnd, PaintEventArgs e) { Graphics g = e.Graphics; /*Font fnt = new Font("Arial", 10); // Draw a string on the PictureBox. g.DrawString("This is a diagonal line drawn on the control", fnt, System.Drawing.Brushes.Blue, new Point(30, 30)); // Draw a line in the PictureBox. g.DrawLine(System.Drawing.Pens.Red, 10, 20,100, 150);*/ if (bCreateCirclePoint == false) { CreateCirclePoint(); } else { } POINT pt; GetCursorPos(out pt); int n = 8; double[] x2 = new double[n]; double[] y2 = new double[n]; GetCirclePoint(ref x2, ref y2, n); pt.x = 920; pt.y = 240; //Move(x, y, n,ref x2, ref y2,(double)pt.x, (double)pt.y); RotateMoveScale(x2, y2, n, ref x2, ref y2, angle, (double)pt.x, (double)pt.y, 5); //angle += 10; DrawPolygon(g, x2, y2, n); //DrawPolygon(g, x, y, n); //test5(); } static public void DEBUG_Test3(Form wnd) { POINT p; GetCursorPos(out p); } static public void DEBUG_Test2(Form wnd) { Graphics g = wnd.CreateGraphics(); CreateCirclePoint(); } static public void DEBUG_Test4(Form wnd) { Graphics g = wnd.CreateGraphics(); Rectangle r = new Rectangle(10, 20, 30, 40); POINT pt; Point p = new Point(); GetCursorPos(out pt); p.X = pt.x; p.Y = pt.y; r.Location = p; r.Width = 10; r.Height = 2; System.Drawing.SolidBrush br = new System.Drawing.SolidBrush(System.Drawing.Color.Red); g.FillRectangle(br, r); } static public void DEBUG_test1(Form wnd, int x, int y) { Graphics g = wnd.CreateGraphics(); Pen pen = new Pen(Color.DarkSeaGreen); g.DrawArc(pen, x, y, 20, 20, 0, 270); } static public void DEBUG_test4(Form wnd, int x, int y) { Graphics g = wnd.CreateGraphics(); Pen pen = new Pen(Color.DarkSeaGreen); g.DrawArc(pen, x, y, 20, 20, 0, 270); } }