zoukankan      html  css  js  c++  java
  • 33、插入一段大学学的计算机,正儿八经的计算机图形学

    1、breaseHam画圆算法

    public void breasenCircle(int a,int b,int r) {
                int x0 = 0;
                int y0 = r;
                int y = y0;
                int x = x0;
                GL.Vertex2(x + a, y + b);
                double p = y * y - 2 * (r * r - (x + 1) * (x + 1)) + (y - 1) * (y - 1);
                while(x<=x0+(Math.Sqrt(2)/2)*r){
                    if (p > 0)
                    {
                        p = p + 4 * (x - y) + 10;
                        x = x + 1;
                        y = y - 1;
                        GL.Vertex2(x+a, y+b);
                        GL.Vertex2(y+a, x+b);
                        GL.Vertex2(x+a,-y+b);
                        GL.Vertex2(-y+a,x+b);
                        GL.Vertex2(-x+a,-y+b);
                        GL.Vertex2(-y+a,-x+b);
                        GL.Vertex2(-x+a, y+b);
                        GL.Vertex2(y+a, -x+b);
                    }
                    else
                    {
                       
                        p = p + 4 * x + 6;
                        x = x + 1;
                        GL.Vertex2(x+a, y+b);
                        GL.Vertex2(y+a, x+b);
                        GL.Vertex2(x+a, -y+b);
                        GL.Vertex2(-y+a, x+b);
                        GL.Vertex2(-x+a, -y+b);
                        GL.Vertex2(-y+a, -x+b);
                        GL.Vertex2(-x+a, y+b);
                        GL.Vertex2(y+a, -x+b);
                    }
                    
                }
            }

    2、breaseHam画直线

    public void Bresenhamline(int x0, int y0, int x1, int y1) {
                double dx = x1 - x0;
                double dy = y1 - y0;
                int x=x0;
                int y=y0;
                double b = (y0 * x1 - y1 * x0) / (x1 - x0);
                if (Math.Abs(dy / dx) <= 1)
                {
                    double p = 2 * dx * y - 2 * dy * x - 2 * dy - 2 * b * dx + dx;
                    while (Math.Abs(x1 - x) > 1)
                    {
                        if (p > 0)
                        {
                            x = x + 1;
                            GL.Vertex2(x, y);
                            p = p - 2 * dy;
                        }
                        else
                        {
                            x = x + 1;
                            y = y + 1;
                            p = p + 2 * dx - 2 * dy;
                            GL.Vertex2(x, y);
                        }
                    }
                }
                else {
                    double p = 2 * x * dy - 2 * dx * y + dy - 2 * dx + 2 * dx * b;
                    while (Math.Abs(y1 - y) > 1) {
                        if (p > 0)
                        {
                            y = y + 1;
                            GL.Vertex2(x, y);
                            p = p - 2 * dx;
                        }
                        else {
                            x = x + 1;
                            y = y + 1;
                            GL.Vertex2(x,y);
                            p = p + 2 * dy - 2 * dx;
                        }
                    }
                    
                }
               
            }

    3、中点画线算法

    public void midLine(int x0,int y0,int x1,int y1) {
                double a = y0 - y1;
                double b = x1 - x0;
                int x = x0;
                int y = y0;
                GL.Vertex2(x,y);
                if (Math.Abs(a / b) < 1)
                {
                    double d = 2 * a + b;
                    while (x < x1)
                    {
                        if (d < 0)
                        {
                            x = x + 1;
                            y = y + 1;
                            GL.Vertex2(x, y);
                            d = d + a + b;
                        }
                        else
                        {
                            x = x + 1;
                            GL.Vertex2(x, y);
                            d = d + 2 * a;
                        }
                    }
                }
                else {
                    double d = a + 2 * b;
                    while(y<y1){
                        if (d < 0)
                        {
                            y = y + 1;
                            GL.Vertex2(x, y);
                            d = d + 2 * b;
                        }
                        else
                        {
                            y = y + 1;
                            x = x + 1;
                            GL.Vertex2(x,y);
                            d = d + 2 * a + 2 * b;
                        }
                    }
                }
            }

    4、DDA画线算法

    public void DDALine(int x0,int y0,int x1,int y1) {
                  float dx, dy, y, k,x;
                  dx = x1 - x0; dy = y1 - y0;
                  k = dy / dx; y = y0; x = x0;
                  if (k <= 1)
                  {
                      for (x = x0; x < x1; x++)
                      {
                          GL.Vertex2(x, Math.Floor(y + 0.5));
                          y = y + k;
                      }
                  }
                  else {
                      for (y = y0; y < y1; y++)
                      {
                          GL.Vertex2(Math.Floor(x + 0.5), y);
                          x = x + 1 / k;
                      }
                  }
                  
            }

    二、直线和圆的绘制

    private void glControl1_Paint(object sender, PaintEventArgs e)
            {
                if (!loaded)
                    return;
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                GL.MatrixMode(MatrixMode.Modelview);
                GL.LoadIdentity();
                GL.Color3(Color.Green);
                GL.Begin(BeginMode.Polygon);
                GL.Vertex2(1, 1);
                GL.Vertex2(20, 80);
                GL.Vertex2(30, 200);
                GL.Vertex2(50, 180);
                GL.Vertex2(60, 50);
                GL.End();
                allPaint8(50, 160, Color.Green, Color.Red);
                //paintGraphy();
                GL.End();
                glControl1.SwapBuffers();
            }
     //八联通递归填充算法
            public void allPaint8(int x, int y, Color boundry, Color fillColor)
            {
    
                if (Getpixel(x, y).ToArgb() != fillColor.ToArgb() && Getpixel(x, y).ToArgb() == boundry.ToArgb())
                {
    
                    GL.Color3(fillColor);
                    GL.Begin(BeginMode.Points);
                    GL.Vertex2(x, y);
                    GL.End();
                    allPaint8(x - 1, y - 1, boundry, fillColor);
                    allPaint4(x, y - 1, boundry, fillColor);
                    allPaint8(x + 1, y - 1, boundry, fillColor);
                    allPaint8(x - 1, y, boundry, fillColor);
                    allPaint4(x + 1, y, boundry, fillColor);
                    allPaint4(x, y + 1, boundry, fillColor);
                    allPaint8(x - 1, y + 1, boundry, fillColor);
                    allPaint8(x + 1, y + 1, boundry, fillColor);
                    
                }
    
            }
     //四联通递归填充算法
            public void allPaint4(int x,int y,Color boundry,Color fillColor) {
                if (Getpixel(x, y).ToArgb()!= fillColor.ToArgb() && Getpixel(x, y).ToArgb()==boundry.ToArgb())
                {
                   
                    GL.Color3(fillColor);
                    GL.Begin(BeginMode.Points);
                    GL.Vertex2(x, y);
                    GL.End();
                    allPaint4(x - 1, y,boundry,fillColor);
                    allPaint4(x, y + 1, boundry, fillColor);
                    allPaint4(x + 1, y, boundry, fillColor);
                    allPaint4(x, y - 1, boundry, fillColor); 
                }
    
            }
    多边形扫描线填充:
     
    填充之前的图像如上所示:
    填充之后的图像如下所示:
     
     private void glControl1_Paint(object sender, PaintEventArgs e)
            {
                if (!loaded)
                    return;
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                GL.MatrixMode(MatrixMode.Modelview);
                GL.LoadIdentity();
                GL.Color3(Color.Green);
                GL.Begin(BeginMode.Points);
                GL.Vertex2(1, 1);
                GL.Vertex2(20, 80);
                GL.Vertex2(30, 200);
                GL.Vertex2(50, 180);
                GL.Vertex2(60, 50);
                GL.End();
                paintGraphy();
                GL.End();
                glControl1.SwapBuffers();
            }
    
    //多边形扫描线算法
            public void paintGraphy() {
                NList nlist = new NList(1);
                GL.Vertex2(1, 1);
                GL.Vertex2(20, 80);
                nlist.Add(new Node(1, (20.0 - 1.0) / (80.0 - 1.0), 80));
                GL.Vertex2(30, 200);
                nlist.Add(new Node(20, (30.0 - 20.0) / (200.0 - 80.0), 200));
                GL.Vertex2(50, 180);
                nlist.Add(new Node(50, (50.0 - 30.0) / (180.0 - 200.0), 200));
                GL.Vertex2(60, 50);
                nlist.Add(new Node(60, (60.0 - 50.0) / (50.0 - 180.0), 180));
                nlist.Add(new Node(1, (60.0 - 1.0) / (50.0 - 1.0), 50));
                double t = 0;
                for (int i = 1; i <= 200; i++) {
                    List<Point> listPoint = new List<Point>();
                   
                    for (int j = 0; j < nlist.myndL.Count; j++) {
                        if (j == 0) {
                            if (i > 1 && i <= nlist.myndL[0].ymax) {
                                listPoint.Add(new Point(nlist.myndL[j].x + i*nlist.myndL[0].dx, i));
    
                            }
                        }
                        else if (j == 1)
                        {
                            if (i > 1 && i <= nlist.myndL[1].ymax)
                            {
                                listPoint.Add(new Point(nlist.myndL[0].x + i*nlist.myndL[1].dx, i));
    
                            }
    
                        }
                        else if (j == 2) {
                            if (i > nlist.myndL[0].ymax && i <= nlist.myndL[j + 1].ymax) {
                                t = i - nlist.myndL[0].ymax;
                                listPoint.Add(new Point(nlist.myndL[j].x + t * nlist.myndL[j].dx, i));
                            }
                        }
                        else
                        {
                            double YMAX;
                            if (j + 1 == nlist.myndL.Count)
                            {
                                YMAX = nlist.myndL[1].ymax;
                            }
                            else { 
                                YMAX=nlist.myndL[j + 1].ymax;
                            }
                            double valueJ_1 = Math.Min(YMAX, nlist.myndL[j].ymax);
                            double valueUp = Math.Max(YMAX, nlist.myndL[j].ymax);
                            if (i >valueJ_1 && i <= valueUp )
                            {
                                t = i - valueJ_1;
                                listPoint.Add(new Point(nlist.myndL[j].x + t * nlist.myndL[j].dx, i));
    
                            }
                        }
                       
    
                    }
                    if (listPoint.Count == 2)
                    {
                        Bresenhamline(listPoint[0].x, listPoint[0].y, listPoint[1].x, listPoint[1].y);
                    }
                }
            }
    1.    图形的二维旋转
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using OpenTK.Graphics.OpenGL;
    namespace mywapp
    {
        public partial class Form1 : Form
        {
            bool loaded = false;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void glControl1_Load(object sender, EventArgs e)
            {
                loaded = true;
                GL.ClearColor(Color.White);
                SetupViewport();
            }
            private void SetupViewport()
            {
                int w = glControl1.Width;
                int h = glControl1.Height;
                GL.MatrixMode(MatrixMode.Projection);
                GL.LoadIdentity();
                GL.Ortho(0, w, 0, h, -1, 1); // Bottom-left corner pixel has coordinate (0, 0)
                GL.Viewport(0, 0, w, h); // Use all of the glControl painting area
            }
    
    private void glControl1_Paint(object sender, PaintEventArgs e)
            {
                if (!loaded)
                    return;
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                GL.MatrixMode(MatrixMode.Modelview);
                GL.LoadIdentity();
                GL.Color3(Color.Green);
                GL.Begin(BeginMode.LineLoop);
                GL.Vertex2(1, 1);
                GL.Vertex2(20, 80);
                GL.Vertex2(30, 200);
                GL.Vertex2(50, 180);
                GL.Vertex2(60, 50);
                List<Point> list = new List<Point>();
                list.Add(new Point(1, 1));
                list.Add(new Point(20,80));
                list.Add(new Point(30,200));
                list.Add(new Point(50, 180));
                list.Add(new Point(60, 50));
                List<Point> nlist = rotato2(-30, list);
                foreach (Point point in nlist) {
                    GL.Vertex2(point.x,point.y);
                }
                GL.End();
                glControl1.SwapBuffers();
            }
    
    //二维旋转
            private List<Point> rotato2(int angel, List<Point> points)
            { 
                double o=(angel*Math.PI)/180.0;
                double[,] a = new double[,] { { Math.Cos(o), -Math.Sin(o) }, { Math.Sin(o), Math.Cos(o) } };
                foreach (Point point in points) {
                    point.x = a[0, 0] * point.x + a[0, 1] * point.y;
                    point.y = a[1, 0] * point.x + a[1, 1] * point.y;
                }
                return points;
    
            }
    二维平移
    将原图形在x轴上向右平移80个单位像素,在y轴上向上平移20个单位像素
     
    //二维平移
            private List<Point> moveLocation(int x, int y, List<Point> points) {
                foreach (Point point in points)
                {
                    point.x = point.x + x;
                    point.y = point.y + y;
                }
                return points;
            }
    二维缩放
    将原图形放大了1.3倍之后,在x轴上向右平移了60个单位像素后,得到的图形:
     
    //二维缩放
            private List<Point> changeSize2(double size, List<Point> points) {
                double[,] a = new double[,] { { size, 0 }, { 0, size } };
                foreach (Point point in points)
                {
                    point.x = a[0, 0] * point.x + a[0, 1] * point.y;
                    point.y = a[1, 0] * point.x + a[1, 1] * point.y;
                }
                return points;
            }

    最后是超级牛逼的贝塞尔曲线

     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using OpenTK.Graphics.OpenGL;
    namespace mywapp
    {
        public partial class Form1 : Form
        {
            bool loaded = false;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void glControl1_Load(object sender, EventArgs e)
            {
                loaded = true;
                GL.ClearColor(Color.White);
                SetupViewport();
            }
            private void SetupViewport()
            {
                int w = glControl1.Width;
                int h = glControl1.Height;
                GL.MatrixMode(MatrixMode.Projection);
                GL.LoadIdentity();
                GL.Ortho(0, w, 0, h, -1, 1); // Bottom-left corner pixel has coordinate (0, 0)
                GL.Viewport(0, 0, w, h); // Use all of the glControl painting area
            }
    
            private void glControl1_Paint(object sender, PaintEventArgs e)
            {
                if (!loaded)
                    return;
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                GL.MatrixMode(MatrixMode.Modelview);
                GL.LoadIdentity();
                GL.Color3(Color.Green);
                GL.Begin(BeginMode.Points);
                GL.Vertex2(10, 30);
                GL.Vertex2(40, 60);
                GL.Vertex2(70, 50);
                GL.Vertex2(120, 100);
                GL.End();
                int[,] shuzu = new int[4, 2] { { 10, 30 }, { 40, 60 }, { 70, 50 }, { 120, 110 } };
                drawBezierCurve(shuzu);
                glControl1.SwapBuffers();
            }
    
            //Bezier画曲线算法
            public void drawBezierCurve(int[,] shuzu)
            {
                int n = 0;
                if ((n = shuzu.GetLength(0)) < 2)
                    return;
                for (double t = 0.0; t <= 1; t += 0.05 / n)
                {
                    double[,] pshuzu = new double[1, 2] { { 0, 0 } };
                    computeP(shuzu, n - 1, t, pshuzu);
                    GL.Color3(Color.Red);
                    GL.Begin(BeginMode.Points);
                    GL.Vertex2(pshuzu[0, 0], pshuzu[0, 1]);
                    GL.End();
                }
            }
            void computeP(int[,] pshuzu, int count, double u, double[,] p)
            {
                int i = 0;
                int k = 0;
                double[,] Pshuzu = new double[count + 1, 2];
    
                for (i = 0; i <= count; i++)
                {
                    Pshuzu[i, 0] = pshuzu[i, 0];
                    Pshuzu[i, 1] = pshuzu[i, 1];
                }
                for (k = 1; k <= count; k++)
                {
                    for (i = 0; i <= count - k; i++)
                    {
                        Pshuzu[i, 0] = ((1 - u) * Pshuzu[i, 0] + u * Pshuzu[i + 1, 0]);
                        Pshuzu[i, 1] = ((1 - u) * Pshuzu[i, 1] + u * Pshuzu[i + 1, 1]);
                    }
                }
                p[0, 0] = Pshuzu[0, 0];
                p[0, 1] = Pshuzu[0, 1];
           }
    }
    }

    今天是2016年10月18号,今天心情超级不开心

  • 相关阅读:
    回车与换行的区别
    C# 验证数字
    FCKeditor 2.6.6在ASP中的安装及配置方法分享--ZZ转载自网络
    关于Application.Lock…Application.Unlock有什么作用?
    关于Application.Lock和Lock(obj)
    C#解决Linq OrderBy() 失效的小技巧
    文件夹添加 IIS 应用程序池用户权限
    we7调用模板如何区分栏目页与详细页
    第二阶段冲刺(第十天)
    每周总结(第十六周)
  • 原文地址:https://www.cnblogs.com/weizhen/p/5971967.html
Copyright © 2011-2022 走看看