public void SetPixel(int x, int y, MyColor color) { var index = (y * Width + x) * 4; if (index >= Bits.Length || index < 0) { return; } Bits[index] = color.B; Bits[index + 1] = color.G; Bits[index + 2] = color.R; }
public void DrawCurve(List <Vector2> parameters, DirectBitmap bitmap) { var m = bitmap.Width; var mult = (int)(m / (Math.PI * 2)); var points = parameters.Select(x => new Point((int)(x.X * mult), (int)(x.Y * mult))).ToList(); var pointsClamped = points.Select(x => new Point(((x.X % m) + m) % m, ((x.Y % m) + m) % m)).ToList(); var myColor = new MyColor(255, 255, 255); var lines = pointsClamped.Zip(pointsClamped.Skip(1), (x, y) => Tuple.Create(x, y)) .Where(x => (x.Item1.X - x.Item2.X) * (x.Item1.X - x.Item2.X) + (x.Item1.Y - x.Item2.Y) * (x.Item1.Y - x.Item2.Y) < 30).ToList(); lines.ForEach(x => bitmap.DrawLine(x.Item1, x.Item2, myColor, false)); }
private void DrawSinglePass(Matrix4x4 perspectiveMatrix, MyColor color, bool shouldAppend) { var viewportMatrix = GetViewportMatrix(_bitmap.Width, _bitmap.Height); var viewMatrix = _camera.ViewMatrix; var matrixViewProj = viewMatrix * perspectiveMatrix; foreach (var ob in _objectsController.DrawableObjects) { var curve = ob as ICurve; var mv = ob.GetModelMatrix() * viewMatrix; var tuple = ob.GetLines(); var pointsLines = tuple.Item2; var lineList = tuple.Item1.ToList(); var linesToDraw = GetLinesToDraw(perspectiveMatrix, pointsLines, mv, ob, lineList, viewportMatrix); if (curve == null || curve.DrawLines) { linesToDraw.Where(x => x.Item3).ToList(). ForEach(l => _bitmap.DrawLine(new Point((int)l.Item1.X, (int)l.Item1.Y), new Point((int)l.Item2.X, (int)l.Item2.Y), color, shouldAppend)); } if (curve == null || linesToDraw.Count == 0) { continue; } var points = linesToDraw.Select(x => x.Item1).Concat(linesToDraw.Select(x => x.Item2)).ToList(); var minX = points.Select(p => p.X).Min(Clamp); var maxX = points.Select(p => p.X).Max(Clamp); var minY = points.Select(p => p.Y).Min(Clamp); var maxY = points.Select(p => p.Y).Max(Clamp); var count = maxX - minX + maxY - minY; if (count == 0) { continue; } var pointsToDraw = curve.GetPoints(count); DrawSimplePoints(color, pointsToDraw, matrixViewProj, viewportMatrix, _bitmap); } DrawPoints(color, matrixViewProj, viewportMatrix); }
private void DrawPoint(MyColor color, Matrix4x4 matrixViewProj, Matrix4x4 viewportMatrix, Vector4 point, Graphics g = null, Pen pen = null, Pen secondPen = null, Pen thirdPen = null, DrawablePoint pointData = null, bool circle = false) { var transformed = Vector4.Transform(point, matrixViewProj); var p = transformed / transformed.W; if (pointData != null) { pointData.ScreenPosition = new Point(-100, -100); } if (p.X > -1 && p.X < 1 && p.Y > -1 && p.Y < 1 && p.Z > -1 && p.Z < 1) { var screenPoint = Vector4.Transform(p, viewportMatrix); if (screenPoint.X < 2 || screenPoint.X > _bitmap.Width - 2 || screenPoint.Y < 2 || screenPoint.Y > _bitmap.Height - 2) { return; } var x = (int)screenPoint.X; var y = (int)screenPoint.Y; _bitmap.SetPixel(x - 1, y, color); _bitmap.SetPixel(x, y + 1, color); _bitmap.SetPixel(x + 1, y, color); _bitmap.SetPixel(x, y - 1, color); _bitmap.SetPixel(x, y, color); if (pointData != null && thirdPen != null) { pointData.ScreenPosition = new Point(x, y); if (pointData.IsCorner) { g.DrawEllipse(thirdPen, screenPoint.X - 6, screenPoint.Y - 6, 12, 12); } } if (circle && g != null && pen != null) { g.DrawEllipse(pen, screenPoint.X - 5, screenPoint.Y - 5, 10, 10); } if (!circle && g != null && secondPen != null) { g.DrawEllipse(secondPen, screenPoint.X - 5, screenPoint.Y - 5, 10, 10); } } }
public void DrawLine(Point p1, Point p2, MyColor color, bool addColors = false) { if (addColors) { AppendLine(p1, p2, color); return; } var differencePoint = new Point(p2.X - p1.X, p2.Y - p1.Y); var octant = FindOctant(differencePoint); var mappedDifference = MapInput(octant, differencePoint.X, differencePoint.Y); var dx = mappedDifference.X; var dy = mappedDifference.Y; var d = 2 * dy - dx; var y = 0; for (int x = 0; x <= mappedDifference.X; x++) { var p = MapOutput(octant, x, y); var newPointX = p.X + p1.X; var newPointY = p.Y + p1.Y; if (newPointX >= 0 && newPointX < Width && newPointY >= 0 && newPointY < Height) { var index = (newPointY * Width + newPointX) * 4; Bits[index] = color.B; Bits[index + 1] = color.G; Bits[index + 2] = color.R; } if (d > 0) { y = y + 1; d = d - 2 * dx; } d = d + 2 * dy; } }
private void DrawPoints(MyColor color, Matrix4x4 matrixViewProj, Matrix4x4 viewportMatrix) { var g = Graphics.FromImage(_bitmap.Bitmap); var pen = Pens.Yellow; var secondPen = Pens.Green; var thirdPen = Pens.BlueViolet; foreach (var point in _objectsController.Points) { DrawPoint(color, matrixViewProj, viewportMatrix, point.Point, g, pen, secondPen, thirdPen, point, point.Selected); } //var p2 = _objectsController.DrawableObjects.OfType<BasicSurface>() // .SelectMany(x => new[] { x.GetPoint(0.0f, 0.5f), x.GetPoint(0.0f, 0.5f) + x.Du(0.0f, 0.5f) }) // .Select(x => new DrawablePoint(x.X, x.Y, x.Z)).ToList(); //p2.ForEach(x => DrawPoint(color, matrixViewProj, viewportMatrix, x.Point, g, pen, secondPen, thirdPen, x, true)); g.Dispose(); }
private void AppendLine(Point p1, Point p2, MyColor color) { var differencePoint = new Point(p2.X - p1.X, p2.Y - p1.Y); var octant = FindOctant(differencePoint); var mappedDifference = MapInput(octant, differencePoint.X, differencePoint.Y); var dx = mappedDifference.X; var dy = mappedDifference.Y; var d = 2 * dy - dx; var y = 0; for (int x = 0; x <= mappedDifference.X; x++) { var p = MapOutput(octant, x, y); var newPointX = p.X + p1.X; var newPointY = p.Y + p1.Y; if (newPointX >= 0 && newPointX < Width && newPointY >= 0 && newPointY < Height) { var index = (newPointY * Width + newPointX) * 4; var blue = (byte)Math.Min(255, Bits[index] + color.B); var green = (byte)Math.Min(255, Bits[index + 1] + color.G); var red = (byte)Math.Min(255, Bits[index + 2] + color.R); Bits[index] = blue; Bits[index + 1] = green; Bits[index + 2] = red; } if (d > 0) { y = y + 1; d = d - 2 * dx; } d = d + 2 * dy; } }