public bool IsOnBorder(FigurePoint p, PolygonFigure pf) { var points = pf.Points; bool onBorder = false; for (int i = 0; i < points.Count; i++) { int j = (i + 1) % points.Count; var p1 = points[i]; var p2 = points[j]; if (p1.x - p2.x == 0 && p1.x == p.x) { onBorder = p1.y < p2.y ? (p.y >= p1.y && p.y <= p2.y) : (p.y >= p2.y && p.y <= p1.y); } else if (p1.y - p2.y == 0 && p1.y == p.y) { onBorder = p1.x < p2.x ? (p.x >= p1.x && p.x <= p2.x) : (p.x >= p2.x && p.x <= p1.x); } else { var lc = new LineCutter(points[i], points[j], p, p); if (lc.SimpleCutYFormule(p.x) == p.y) { onBorder = p1.x < p2.x ? (p.x >= p1.x && p.x <= p2.x) : (p.x >= p2.x && p.x <= p1.x); } } if (onBorder) { return(true); } } return(false); }
private bool IsInPoligon(FigurePoint point, PolygonFigure pf) { int i1, i2, n, N, S, S1, S2, S3; bool flag = false; N = pf.Points.Count; var p = pf.Points; for (n = 0; n < N; n++) { flag = false; i1 = n < N - 1 ? n + 1 : 0; while (!flag) { i2 = i1 + 1; if (i2 >= N) { i2 = 0; } if (i2 == (n < N - 1 ? n + 1 : 0)) { break; } S = Math.Abs(p[i1].x * (p[i2].y - p[n].y) + p[i2].x * (p[n].y - p[i1].y) + p[n].x * (p[i1].y - p[i2].y)); S1 = Math.Abs(p[i1].x * (p[i2].y - point.y) + p[i2].x * (point.y - p[i1].y) + point.x * (p[i1].y - p[i2].y)); S2 = Math.Abs(p[n].x * (p[i2].y - point.y) + p[i2].x * (point.y - p[n].y) + point.x * (p[n].y - p[i2].y)); S3 = Math.Abs(p[i1].x * (p[n].y - point.y) + p[n].x * (point.y - p[i1].y) + point.x * (p[i1].y - p[n].y)); if (S == S1 + S2 + S3) { flag = true; break; } i1 = i1 + 1; if (i1 >= N) { i1 = 0; } } if (!flag) { break; } } return(flag); }
private PolygonFigure GenerateClippedFigure(PolygonFigure clippingFigure) { int i; var clippedFigure = new PolygonFigure(Points.Select(p => p.ToPoint()).ToList()); i = 0; while (i < clippedFigure.Points.Count) { int j = (i + 1) % clippedFigure.Points.Count; var cutPoints = FindCutPoints(clippedFigure.Points[i], clippedFigure.Points[j], clippingFigure); clippedFigure.Points.InsertRange(j, cutPoints); i += cutPoints.Count + 1; } for (i = 0; i < clippedFigure.Points.Count; i++) { if (IsInPoligon(clippedFigure.Points[i], clippingFigure)) { clippedFigure.Points[i].lineType = LineDrawType.Broken; } else { clippedFigure.Points[i].lineType = LineDrawType.Solid; } } for (i = 0; i < clippedFigure.Points.Count; i++) { if (IsOnBorder(clippedFigure.Points[i], clippingFigure)) { int j = i == 0 ? j = clippedFigure.Points.Count - 1 : i - 1; int k = (i + 1) % clippedFigure.Points.Count; if (clippedFigure.Points[j].lineType == clippedFigure.Points[k].lineType) { clippedFigure.Points[i].lineType = clippedFigure.Points[j].lineType; } else if (clippedFigure.Points[j].lineType == LineDrawType.Solid) { clippedFigure.Points[i].lineType = LineDrawType.Broken; } else { clippedFigure.Points[i].lineType = LineDrawType.Solid; } } } return(clippedFigure); }
public void DrawClippedFigure(PolygonFigure clippingFigure, DrawLine visLineFunc, DrawLine unvisLineFunc) { var figure = GenerateClippedFigure(clippingFigure); var points = figure.Points; for (int i = 0; i < points.Count; i++) { var j = (i + 1) % points.Count; if (points[i].lineType == LineDrawType.Solid) { visLineFunc(points[i].x, points[i].y, points[j].x, points[j].y, Color.Black); } else { unvisLineFunc(points[i].x, points[i].y, points[j].x, points[j].y, Color.Black); } } }
private List <FigurePoint> FindCutPoints(FigurePoint p1, FigurePoint p2, PolygonFigure pf) { List <FigurePoint> list = new List <FigurePoint>(); for (int i = 0; i < pf.Points.Count; i++) { int j = (i + 1) % pf.Points.Count; list.Add(FindCutPoint(pf.Points[i], pf.Points[j], p1, p2)); } if (p2.x < p1.x) { list.Sort(new DecPointComparer()); list = list.Where(p => p.x != -1).ToList(); } else { list.Sort(new IncPointComparer()); list = list.Where(p => p.x != -1).ToList(); } return(list); }
public MainForm() { InitializeComponent(); Thread thread = new Thread(() => { SDL.SDL_Init(SDL.SDL_INIT_VIDEO); IntPtr wnd = SDL.SDL_CreateWindow( "АКГ: Лабораторная работа №1", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL.SDL_WindowFlags.SDL_WINDOW_SHOWN ); renderer = SDL.SDL_CreateRenderer(wnd, -1, SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED); List <Point> list = new List <Point>(); list.Add(new Point(200, 200)); list.Add(new Point(300, 200)); list.Add(new Point(300, 300)); list.Add(new Point(200, 300)); var figure = new PolygonFigure(list, new Point(250, 250)); list.Clear(); list.Add(new Point(300, 300)); list.Add(new Point(400, 300)); list.Add(new Point(400, 400)); list.Add(new Point(300, 400)); var figure2 = new PolygonFigure(list, new Point(350, 350)); bool quit = false; while (!quit) { SDL.SDL_Event sdlEvent; SDL.SDL_PollEvent(out sdlEvent); switch (sdlEvent.type) { case SDL.SDL_EventType.SDL_QUIT: { quit = true; break; } case SDL.SDL_EventType.SDL_KEYDOWN: { int numkey; var ptr = SDL.SDL_GetKeyboardState(out numkey); if (Marshal.ReadByte(ptr + (int)SDL.SDL_Scancode.SDL_SCANCODE_W) == 1) { figure.MoveY(-DY); } if (Marshal.ReadByte(ptr + (int)SDL.SDL_Scancode.SDL_SCANCODE_S) == 1) { figure.MoveY(DY); } if (Marshal.ReadByte(ptr + (int)SDL.SDL_Scancode.SDL_SCANCODE_A) == 1) { figure.MoveX(-DX); } if (Marshal.ReadByte(ptr + (int)SDL.SDL_Scancode.SDL_SCANCODE_D) == 1) { figure.MoveX(DX); } if (Marshal.ReadByte(ptr + (int)SDL.SDL_Scancode.SDL_SCANCODE_Q) == 1) { figure.Rotate(-ANGLE); } if (Marshal.ReadByte(ptr + (int)SDL.SDL_Scancode.SDL_SCANCODE_E) == 1) { figure.Rotate(ANGLE); } if (Marshal.ReadByte(ptr + (int)SDL.SDL_Scancode.SDL_SCANCODE_I) == 1) { figure2.MoveY(-DY); } if (Marshal.ReadByte(ptr + (int)SDL.SDL_Scancode.SDL_SCANCODE_K) == 1) { figure2.MoveY(DY); } if (Marshal.ReadByte(ptr + (int)SDL.SDL_Scancode.SDL_SCANCODE_J) == 1) { figure2.MoveX(-DX); } if (Marshal.ReadByte(ptr + (int)SDL.SDL_Scancode.SDL_SCANCODE_L) == 1) { figure2.MoveX(DX); } if (Marshal.ReadByte(ptr + (int)SDL.SDL_Scancode.SDL_SCANCODE_U) == 1) { figure2.Rotate(-ANGLE); } if (Marshal.ReadByte(ptr + (int)SDL.SDL_Scancode.SDL_SCANCODE_O) == 1) { figure2.Rotate(ANGLE); } break; } } int num = 0; SDL.SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); SDL.SDL_RenderClear(renderer); figure.DrawFigure(DrawLine); figure2.DrawClippedFigure(figure, DrawLine, DrawNotVisibleLine); SDL.SDL_RenderPresent(renderer); } SDL.SDL_DestroyRenderer(renderer); SDL.SDL_DestroyWindow(wnd); SDL.SDL_Quit(); }); thread.Start(); thread.Join(); }