无法使用负坐标创建扁平矩形表面

时间:2017-05-28 14:58:47

标签: c# wpf graphics 3d

我正在尝试从文本文件中的坐标绘制一个平面。我正在创建一个矩形网格。我面临的问题是,如果我提供所有正x,y坐标,那么我得到矩形表面。但如果我提供负y坐标和正y坐标的组合,表面就会消失。我无法理解为什么会这样。 这是我的代码:

<Window x:Class="SurfaceCreation.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         
    Title="SurfaceCreation" Height="500" Width="800" WindowState="Maximized">
<Grid>
    <Viewport3D Grid.Row="0" Grid.Column="0"
        Name="MainViewport" >
        <Viewport3D.Camera>
            <PerspectiveCamera Position="200,2,244" LookDirection="0,0,-2" UpDirection="0,1,0"/>
        </Viewport3D.Camera>
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <Model3DGroup>
                    <AmbientLight Color="White"/>
                </Model3DGroup>
            </ModelVisual3D.Content>
        </ModelVisual3D>
    </Viewport3D>
</Grid>
</Window>

我的代码背后文件:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.Windows.Controls;
using System.Linq;
using System.Collections;

namespace SurfaceCreation
{
class Surface
{       
    public string[] rectangles;        
    private Color lineColor = Colors.Black;
    private Color surfaceColor = Colors.Green;
    private Point3D center = new Point3D();
    private bool isHiddenLine = false;
    private bool isWireframe = true;
    private Viewport3D viewport3d = new Viewport3D();

    public bool IsWireframe
    {
        get { return isWireframe; }
        set { isWireframe = value; }
    }
    public bool IsHiddenLine
    {
        get { return isHiddenLine; }
        set { isHiddenLine = value; }
    }
    public Color LineColor
    {
        get { return lineColor; }
        set { lineColor = value; }
    }
    public Color SurfaceColor
    {
        get { return surfaceColor; }
        set { surfaceColor = value; }
    }

    public Point3D Center
    {
        get { return center; }
        set { center = value; }
    }
    public Viewport3D Viewport3d
    {
        get { return viewport3d; }
        set { viewport3d = value; }
    }

    public void CreateSurface()
    {
        string[] lines = System.IO.File.ReadAllLines(@"C:\iitm\sem4\project\C#\CreatePath\Path3D.txt").Skip(4).ToArray();
        int noOfLines = lines.Length;
        ArrayList cLines = new ArrayList();           
        for (int m = 0; m < lines.Length; m++)
        {
            if (lines[m].Length != 0)
            {
                cLines.Add(lines[m]);
            }
        }
        string[] fLines = (String[])cLines.ToArray(typeof(string)); 
        int rowLength = 162;
        int columnLength = 101;         
        rectangles = new string[(rowLength - 1) * (columnLength - 1)];
        Point3D[,] pts = new Point3D[rowLength, columnLength];           
        for (int i = 0; i < rowLength; i++)
        {               
            for (int j = 0; j < columnLength; j++)
            {                    
                string[] separators = { ",",  "!", "?", ";", ":", " ","\t" };
                string point = fLines[i * (columnLength) + j];
                string[] x = point.Split(separators, StringSplitOptions.RemoveEmptyEntries);
                pts[i, j] = new Point3D(Convert.ToDouble(x[0]), Math.Abs(Convert.ToDouble(x[1])), Convert.ToDouble(x[2]));                             
            }
        }
        Point3D[] p = new Point3D[4];
        for (int i = 0; i < 100; i++)
        {
            for (int j = 0; j < 100; j++)
            {
                p[0] = pts[i, j];
                p[1] =pts[i + 1, j];
                p[2] = pts[i + 1, j + 1];
                p[3] = pts[i , j+1];
                //Create rectangular face:
                if (p[2].Z == 0)
                {
                    SurfaceColor = Colors.Gray;
                }
                else
                {
                    SurfaceColor = Colors.Black;
                }
                if (IsHiddenLine == false)
                    Utility.CreateRectangleFace(
                    p[0], p[1], p[2], p[3],
                    SurfaceColor, Viewport3d);
                rectangles[i * (columnLength - 1) + j] = (i * (columnLength - 1) + j).ToString() + "," + p[0].ToString() + "," + p[1].ToString() + "," + p[2].ToString() + "," + p[3].ToString();                    
            }
        }           
    }
}
}

0 个答案:

没有答案