C#中如何通过按下按钮来缩放立方体?

发布于 2024-12-17 17:49:53 字数 2588 浏览 1 评论 0原文

我有一个程序可以打印立方体并且可以旋转。这是代码。

public partial class ProjectorForm : Form
{
    Projector projector;
    Cube cube;
    float deltaRot;           



    public ProjectorForm()
    {            
        InitializeComponent();

    }

    private void ProjectorForm_Load(object sender, EventArgs e)
    {

        deltaRot = 0.01f;

        projector = new Projector();
        cube = new Cube(Vector3.UnitZ * 20*10, 10*10, 10*10, 15*10);

        updateTimer.Start();
    }

    private void updateTimer_Tick(object sender, EventArgs e)
    {
        if (rotXBox.Checked)
            cube.RotateX(deltaRot);
        if (rotYBox.Checked)
            cube.RotateY(deltaRot);
        if (rotZBox.Checked)
            cube.RotateZ(deltaRot);



        doubleBufferedPanel1.Invalidate();
    }



    private void doubleBufferedPanel1_Paint(object sender, PaintEventArgs e)
    {

        e.Graphics.Clear(Color.White);
        cube.Draw(projector, Color.Black, doubleBufferedPanel1.ClientSize.Width, doubleBufferedPanel1.ClientSize.Height, e.Graphics);


    }

    private void button1_Click(object sender, EventArgs e)
    {
        deltaRot = float.Parse(deltaRotBox.Text);
    }




}

class Projector
{
    public Vector3 cameraPosition;
    public float planeDistance;
    ProjectorForm n = new ProjectorForm();





    public Projector()
    {
        cameraPosition = Vector3.Zero;
        planeDistance = 256; //Here, multiply by 2 and the scaled cube, how to make that scale when you press the button.

    }

    public PointF Project(Vector3 point, float width, float height)
    {
        float x = cameraPosition.X + ((cameraPosition.Z + planeDistance) / (point.Z - cameraPosition.Z)) * (point.X - cameraPosition.X) + width / 2;
        float y = cameraPosition.Y + ((cameraPosition.Z + planeDistance) / (point.Z - cameraPosition.Z)) * (point.Y - cameraPosition.Y) + height / 2;
        return new PointF(x, y);
    }

    public void DrawLine(Color color, Vector3 p1, Vector3 p2, float width, float height, Graphics g)
    {
        g.DrawLine(new Pen(color), Project(p1, width, height), Project(p2, width, height));
    }

    public void FillPolygon(Color color, Vector3[] vertices, float width, float height, Graphics g)
    {
        PointF[] points = new PointF[vertices.Length];
        for (int i = 0; i < points.Length; i++)
            points[i] = Project(vertices[i], width, height);
        g.FillPolygon(new SolidBrush(color), points);
    }
}

如何制作立方体可以通过按按钮来缩放。我在Projector类中发现了变量planeDistance,当它增加2倍时,立方体会缩放,但我不知道如何通过按钮来增加它。

I have a program that prints the cube and which can be rotated. Here's the code.

public partial class ProjectorForm : Form
{
    Projector projector;
    Cube cube;
    float deltaRot;           



    public ProjectorForm()
    {            
        InitializeComponent();

    }

    private void ProjectorForm_Load(object sender, EventArgs e)
    {

        deltaRot = 0.01f;

        projector = new Projector();
        cube = new Cube(Vector3.UnitZ * 20*10, 10*10, 10*10, 15*10);

        updateTimer.Start();
    }

    private void updateTimer_Tick(object sender, EventArgs e)
    {
        if (rotXBox.Checked)
            cube.RotateX(deltaRot);
        if (rotYBox.Checked)
            cube.RotateY(deltaRot);
        if (rotZBox.Checked)
            cube.RotateZ(deltaRot);



        doubleBufferedPanel1.Invalidate();
    }



    private void doubleBufferedPanel1_Paint(object sender, PaintEventArgs e)
    {

        e.Graphics.Clear(Color.White);
        cube.Draw(projector, Color.Black, doubleBufferedPanel1.ClientSize.Width, doubleBufferedPanel1.ClientSize.Height, e.Graphics);


    }

    private void button1_Click(object sender, EventArgs e)
    {
        deltaRot = float.Parse(deltaRotBox.Text);
    }




}

class Projector
{
    public Vector3 cameraPosition;
    public float planeDistance;
    ProjectorForm n = new ProjectorForm();





    public Projector()
    {
        cameraPosition = Vector3.Zero;
        planeDistance = 256; //Here, multiply by 2 and the scaled cube, how to make that scale when you press the button.

    }

    public PointF Project(Vector3 point, float width, float height)
    {
        float x = cameraPosition.X + ((cameraPosition.Z + planeDistance) / (point.Z - cameraPosition.Z)) * (point.X - cameraPosition.X) + width / 2;
        float y = cameraPosition.Y + ((cameraPosition.Z + planeDistance) / (point.Z - cameraPosition.Z)) * (point.Y - cameraPosition.Y) + height / 2;
        return new PointF(x, y);
    }

    public void DrawLine(Color color, Vector3 p1, Vector3 p2, float width, float height, Graphics g)
    {
        g.DrawLine(new Pen(color), Project(p1, width, height), Project(p2, width, height));
    }

    public void FillPolygon(Color color, Vector3[] vertices, float width, float height, Graphics g)
    {
        PointF[] points = new PointF[vertices.Length];
        for (int i = 0; i < points.Length; i++)
            points[i] = Project(vertices[i], width, height);
        g.FillPolygon(new SolidBrush(color), points);
    }
}

How to make a cube can be scaled by pressing a button. I found the variable planeDistance in the class Projector, when it increased by 2 times the cube is scaled, but I do not know how it can be increased by means of a button.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷清清 2024-12-24 17:49:53

字段planeDistance是公共的,因此您可以从类外部更改它。即只需将如下内容添加到按钮的事件处理程序中:

projector.planeDistance += 10; // Change 10 as appropriate

值得注意的是,这不会更改立方体的大小,它会更改相机与其之间的距离。因此,虽然立方体的大小似乎在变化,但这只是因为相机移近/移远。


要实际更改立方体的大小,您必须更改 cube 类中的字段。
由于大小是由构造函数中创建的向量定义的,因此在创建立方体后,您实际上没有一种简单的方法可以更改它们。

  1. 每当您想要更改大小时,您都可以创建一个新的立方体(在表单上的另一个变量中跟踪大小)。
  2. 您可以向多维数据集类添加一个方法,该方法创建定义新大小的新向量(它看起来有点像构造函数,仅填充数组,而不创建它们)。
  3. 您可以向立方体添加一个大小字段,始终创建一个单位立方体 (1, 1, 1),然后在渲染时将每个向量乘以您的大小。

The field planeDistance is public, so you can change it from outside the class. i.e. just add something like the following to the event handler of a button:

projector.planeDistance += 10; // Change 10 as appropriate

It's worth noting that this doesn't change the size of the cube, it changes how far away the camera is from it. So, while the cube appears to be changing in size, that's just because the camera is moving closer / further away.


To actually change the size of the cube you would have to change fields in the cube class.
Since the size is defined by the vectors created in the constructor you don't really have an easy way of changing them once the cube is created.

  1. You could create a new cube whenever you want to change the size (keep track of the size in another variable on the form).
  2. You could add a method to the cube class that creates new vectors that define the new size (it would look a bit like the constructor, only populating the arrays, not creating them).
  3. You could add a size field to your cube, always create a unit cube (1, 1, 1) then when rendering multiply each vector by your size.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文