private void swap(ref PointPol p1, ref PointPol p2) { PointPol c = p1; p1 = p2; p2 = c; }
private Polygon parseString(string s, ref List <PointPol> points, ref int cnt) { List <Edge> e = new List <Edge>(); var prs = s.Split(' ').Select(x => x.Split(';').Select(z => Convert.ToDouble(z)).ToArray()); int wh = pictureBox1.Width / 2; foreach (var y in prs) { PointPol t0 = new PointPol(y[0] + wh, y[1], y[2]); points.Add(t0); } for (int i = cnt + 1; i < points.Count(); ++i) { e.Add(new Edge(i - 1, i)); } e.Add(new Edge(points.Count() - 1, cnt)); cnt = points.Count(); return(new Polygon(e)); }
private void button6_Click(object sender, EventArgs e) { double x1 = Double.Parse(textBoxX1.Text.ToString()); double y1 = Double.Parse(textBoxY1.Text.ToString()); double z1 = Double.Parse(textBoxZ1.Text.ToString()); double x2 = Double.Parse(textBoxX2.Text.ToString()); double y2 = Double.Parse(textBoxY2.Text.ToString()); double z2 = Double.Parse(textBoxZ2.Text.ToString()); double angle = Double.Parse(textBoxAngle.Text.ToString()); PointPol p1 = new PointPol(x1, y1, z1); PointPol p2 = new PointPol(x2, y2, z2); foreach (var f in figures) { f.rotate(Tuple.Create(p1, p2), angle); } print_figures(); }
public PointPol rotate(Tuple <PointPol, PointPol> direction, double angle, double a, double b, double c) { double phi = Math.PI / 360 * angle; PointPol p = shift(-a, -b, -c); double x1 = direction.Item1.X; double y1 = direction.Item1.Y; double z1 = direction.Item1.Z; double x2 = direction.Item2.X; double y2 = direction.Item2.Y; double z2 = direction.Item2.Z; double vecx = x2 - x1; double vecy = y2 - y1; double vecz = z2 - z1; double len = Math.Sqrt(vecx * vecx + vecy * vecy + vecz * vecz); double l = vecx / len; double m = vecy / len; double n = vecz / len; double[,] transfer = new double[4, 4] { { l *l + Math.Cos(phi) * (1 - l * l), l *(1 - Math.Cos(phi)) * m + n * Math.Sin(phi), l *(1 - Math.Cos(phi)) * n - m * Math.Sin(phi), 0 }, { l *(1 - Math.Cos(phi)) * m - n * Math.Sin(phi), m *m + Math.Cos(phi) * (1 - m * m), m *(1 - Math.Cos(phi)) * n + l * Math.Sin(phi), 0 }, { l *(1 - Math.Cos(phi)) * n + m * Math.Sin(phi), m *(1 - Math.Cos(phi)) * n - l * Math.Sin(phi), n *n + Math.Cos(phi) * (1 - n * n), 0 }, { 0, 0, 0, 1 } }; var t1 = matrix_multiplication(p.getP(), transfer); t1 = matrix_multiplication(t1, transfer); PointPol p2 = translatePol(t1); PointPol p3 = p2.shift(a, b, c); return(p3); }
private void DrawShadedTriangle(PointPol p0, PointPol p1, PointPol p2, string s, ref List <Tuple <PointPol, double> > pixels) { PointPol P0 = p0; PointPol P1 = p1; PointPol P2 = p2; // Сортировка точек так, что y0 <= y1 <= y2 if (P1.Y < P0.Y) { swap(ref P1, ref P0); } if (P2.Y < P0.Y) { swap(ref P2, ref P0); } if (P2.Y < P1.Y) { swap(ref P2, ref P1); } int z0 = (int)P0.Z; /*if (z0 != 0) * z0 = 1 / z0 * 1000000; */ int z1 = (int)P1.Z; /* if (z1 != 0) * z1 = 1 / z1 * 1000000; * */ int z2 = (int)P2.Z; /* if (z2 != 0) * z2 = 1 / z2 * 1000000; */ // Вычисление координат x и значений h для рёбер треугольника int[] x01 = Interpolate((int)P0.Y, (int)P0.X, (int)P1.Y, (int)P1.X); int[] h01 = Interpolate((int)P0.Y, z0, (int)P1.Y, z1); int[] x12 = Interpolate((int)P1.Y, (int)P1.X, (int)P2.Y, (int)P2.X); int[] h12 = Interpolate((int)P1.Y, z1, (int)P2.Y, z2); int[] x02 = Interpolate((int)P0.Y, (int)P0.X, (int)P2.Y, (int)P2.X); int[] h02 = Interpolate((int)P0.Y, z0, (int)P2.Y, z2); x01 = x01.Take(x01.Count() - 1).ToArray(); int[] x012 = x01.Concat(x12).ToArray(); h01 = h01.Take(h01.Count() - 1).ToArray(); int[] h012 = h01.Concat(h12).ToArray(); int m = x012.Count() / 2; int[] x_left, x_right, h_left, h_right; if (x02[m] < x012[m]) { x_left = x02; x_right = x012; h_left = h02; h_right = h012; } else { x_left = x012; x_right = x02; h_left = h012; h_right = h02; } // Отрисовка горизонтальных отрезков for (int y = (int)P0.Y; y <= (int)P2.Y; ++y) { int x_l = x_left[y - (int)P0.Y]; int x_r = x_right[y - (int)P0.Y]; int[] h_segment = Interpolate(x_l, h_left[y - (int)P0.Y], x_r, h_right[y - (int)P0.Y]); for (int x = x_l; x <= x_r; ++x) { double ourZ = h_segment[x - x_l]; pixels.Add(Tuple.Create(new PointPol(x, y, ourZ), ourZ)); } } }