标题:我们计算机图形学老师发飙了~求帮助~帮我注释一下就成~主要是DDA,Bresenha ...
只看楼主
拉裤兜子
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-6-18
结帖率:100%
已结贴  问题点数:20 回复次数:1 
我们计算机图形学老师发飙了~求帮助~帮我注释一下就成~主要是DDA,Bresenham等基本算法
这课我一点也不会~老是留了作业,让我们编好程序之后,再给老师讲解。
我啥也不会,从我们同学那儿要了份代码过来,但是我看不懂。这样下去,到时候老师让我讲我也讲不出来,所以希望坛子里的兄弟们帮我看看这段代码,告诉我到时候怎么给老师说啊~~谢谢了
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsApplication1
{   
    public partial class Form1 : Form
    {
        private bool draw;
        private int type;
        [DllImport("gdi32.dll")]
        private static extern int SetPixel(IntPtr hdc, int x1, int y1, int color);
        public Form1()
        {
            InitializeComponent();
        }
        private uint RGB(Color color)
        {
            uint R = color.R;
            uint G = color.G;
            uint B = color.B;
            G <<= 8;
            B <<= 16;
            return ((uint)(R | G | B));
        }
   
   //各种图形算法==============================================================================================
        //DDA
        private void DDA(int x1, int y1, int x2, int y2, Color color, Graphics g)
        {
            
               
                float x, y, xincre, yincre;
                int k, i;
                k = Math.Abs(x2 - x1);
                if (Math.Abs(y2 - y1) > k)
                    k = Math.Abs(y2 - y1);
                xincre = (float)(x2 - x1) / (float)k;
                yincre = (float)(y2 - y1) / (float)k;
                x = x1;
                y = y1;
                for (i = 1; i <= k; i++)
                {
                    SetPixel(g.GetHdc(), (int)x, (int)y, (int)RGB(color));
                    g.ReleaseHdc();
                    x += xincre;
                    y += yincre;
                }
            
        }
        //Bresenham
        private void Bresenham(Graphics g, int x1, int y1, int x2, int y2, Color c)
        {
            int dx, dy, x, y, p, const1, const2, inc, tmp;
            dx = x2 - x1;
            dy = y2 - y1;
            if (dx * dy >= 0)
                inc = 1;
            else
                inc = -1;
            if (Math.Abs(dx) > Math.Abs(dy))
            {
                if (dx < 0)
                {
                    tmp = x1;
                    x1 = x2;
                    x2 = tmp;
                    tmp = y1;
                    y1 = y2;
                    y2 = tmp;
                    dx = -dx;
                    if (dy < 0)
                    {
                        dy = -dy;
                    }
                }
                p = 2 * dy - dx;
                const1 = 2 * dy;
                const2 = 2 * (dy - dx);
                x = x1;
                y = y1;
                SetPixel(g.GetHdc(), x, y, (int)RGB(c));
                g.ReleaseHdc();
                while (x < x2)
                {
                    x++;
                    if (p < 0)
                        p += const1;
                    else
                    {
                        y += inc;
                        p += const2;
                    }
                    SetPixel(g.GetHdc(), x, y, (int)RGB(c));
                    g.ReleaseHdc();
                }
            }
            else
            {
                if (dy < 0)
                {
                    tmp = x1;
                    x1 = x2;
                    x2 = tmp;
                    tmp = y1;
                    y1 = y2;
                    y2 = tmp;
                    if (dx < 0)
                    {
                        dx = -dx;
                    }
                    dy = -dy;
                }
                p = 2 * dx - dy;
                const1 = 2 * dx;
                const2 = 2 * (dx - dy);
                x = x1;
                y = y1;
                SetPixel(g.GetHdc(), x, y, (int)RGB(c));
                g.ReleaseHdc();
                while (y < y2)
                {
                    y++;
                    if (p < 0)
                        p += const1;
                    else
                    {
                        x += inc;
                        p += const2;
                    }
                    SetPixel(g.GetHdc(), x, y, (int)RGB(c));
                    g.ReleaseHdc();
                }
            }
        }
        //midcircle
        private void midcircle(Graphics g, int xc, int yc, int r,Color c)
        {
            int x;
            int y;
            int p;
            x=0;
            y=r;
            p=1-4*r;
            while(x<y)
            {
                circlepoint(g,xc, yc, x, y, c);
                if(p<0)
                {
                    p+=8*x+12;
                }
                else
                {
                    p+=8*(x-y)+20;
                    y--;
                }
                x++;
            }
        }
        private void circlepoint(Graphics g,int xc, int yc, int x,int y, Color c)
        {
            SetPixel(g.GetHdc(), xc+x, yc+y, (int)RGB(c));
            g.ReleaseHdc();
            SetPixel(g.GetHdc(), xc-x, yc+y, (int)RGB(c));
            g.ReleaseHdc();
            SetPixel(g.GetHdc(), xc+x, yc-y, (int)RGB(c));
            g.ReleaseHdc();
            SetPixel(g.GetHdc(), xc-x, yc-y, (int)RGB(c));
            g.ReleaseHdc();
            SetPixel(g.GetHdc(), xc+y, yc+x, (int)RGB(c));
            g.ReleaseHdc();
            SetPixel(g.GetHdc(), xc-y, yc+x, (int)RGB(c));
            g.ReleaseHdc();
            SetPixel(g.GetHdc(), xc+y, yc-x, (int)RGB(c));
            g.ReleaseHdc();
            SetPixel(g.GetHdc(), xc-y, yc-x, (int)RGB(c));
            g.ReleaseHdc();
           
        }
  //pictureBox==============================================================================================
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics  g=e.Graphics;
            if (draw)
            {
                switch (type)
                {
                    case 1:
                        DDA(20, 50, 680, 580, Color.Blue, g);
                        break;
                    case 2:
                        Bresenham(e.Graphics, 200, 0, 200, 300, Color.OrangeRed);
                        Bresenham(e.Graphics, 0, 150, 400, 150, Color.OrangeRed);
                        Bresenham(e.Graphics, 0, 0, 400, 300, Color.Black);
                        Bresenham(e.Graphics, 400, 0, 0, 300, Color.Black);
                        Bresenham(e.Graphics, 200, 150, 380, 200, Color.Blue);
                        Bresenham(e.Graphics, 200, 150, 270, 310, Color.Blue);
                        Bresenham(e.Graphics, 200, 150, 50, 200, Color.SeaGreen);
                        Bresenham(e.Graphics, 100, 280, 200, 150, Color.SeaGreen);
                        Bresenham(e.Graphics, 150, 0, 200, 150, Color.Purple);
                        Bresenham(e.Graphics, 50, 100, 200, 150, Color.Purple);
                        Bresenham(e.Graphics, 200, 150, 250, 30, Color.Red);
                        Bresenham(e.Graphics, 350, 100, 200, 150, Color.Red);
                        break;
                    case 3:
                        midcircle(g,200, 200, 60, Color.Red);
                        break;
                    default:
                        break;
                }
               
            }
            
        }
//控件响应==============================================================================================
      
        private void button1_Click(object sender, EventArgs e)
        {
            draw = true;
            type = 1;
            pictureBox1.Invalidate();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            draw = true;
            type = 2;
            pictureBox1.Invalidate();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            draw = true;
            type = 3;
            pictureBox1.Invalidate();
        }
//退出==============================================================================================
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
            Application.Exit();
        }
        
        
    }
}
搜索更多相关主题的帖子: 计算机图形学 DDA Bresenham 注释 算法 
2010-06-18 10:25
仙度瑞城
Rank: 2
等 级:论坛游民
帖 子:3
专家分:16
注 册:2010-6-2
得分:14 
哈哈。。。我最近也在忙着写图形学实验。。。真头疼
2010-06-18 18:07



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-310551-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.241000 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved