Tuesday, June 3, 2014

Plotting



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Drawing.Drawing2D; //to use matrix



using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Plotting
{
    public partial class Form1 : Form
    {
        Bitmap bmap1;
        Graphics g1;
        int ymax, xmax;

        //declare graphic state globally
        GraphicsState gs;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            bmap1 = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            g1 = Graphics.FromImage(bmap1);

            xmax = 9 * bmap1.Width / 10; //90% of the bitmap width
            ymax = 9 * bmap1.Height / 10;

            Pen pen = new Pen(Color.White, 2);
            Point p1 = new Point(0, 0);
            Point px2 = new Point(xmax, 0);
            Point py2 = new Point(0, ymax);

            //Create a Matrix to transform the graphics object
            int dx = bmap1.Width / 10;
            int dy = 9 * bmap1.Height / 10;

            //Save Graphics
            gs = g1.Save();

            Matrix matrix = new Matrix(1, 0, 0, -1, dx, dy);

            //apply to this graphics transform
            g1.Transform = matrix;

            g1.DrawLine(pen, p1, px2);
            g1.DrawLine(pen, p1, py2);

            pictureBox1.Image = bmap1;
        }

        private void btnPlotCvsF_Click(object sender, EventArgs e)
        {
            //dividing the x-axis in 10 units
            float x_unit = xmax / 10;
            Pen pen = new Pen(Color.Yellow, 1);

            //draw x-unit
            for (int i = 1; i <= 10; i++)
            {
                PointF p1 = new PointF(i * x_unit, 5);
                PointF p2 = new PointF(i * x_unit, -5);
                g1.DrawLine(pen, p1, p2);
            }
            pictureBox1.Image = bmap1;

            //=================for y axis========================

            float y_unit = ymax / 10;
            for (int i = 1; i <= 10; i++)
            {
                PointF p1 = new PointF(-5, i * y_unit);
                PointF p2 = new PointF(5, i * y_unit);
                g1.DrawLine(pen, p1, p2);
            }

            pictureBox1.Image = bmap1;

            int dx = bmap1.Width / 10;
            int dy = 9 * bmap1.Height / 10;
            Matrix matrix2 = new Matrix(1, 0, 0, 1, dx, dy);
            g1.Transform = matrix2;

            //x unit values
            float sf = (212 - 32) / 10;

            Brush lblbrush = Brushes.White;
            Font lblfont = new Font("Times New Roman", 8, FontStyle.Regular);

            g1.DrawString("(0,32)", lblfont, lblbrush, -15, 8);

            for (int i = 1; i <= 10; i++)
            {
                PointF p = new PointF(i * x_unit, 8);
                double f = sf;
                g1.DrawString((i * f + 32).ToString(), lblfont, lblbrush, p);
            }

            //draw y unit values
            //for (int i = 10; i <= 100; i += 10)
            //{
            //    PointF p = new PointF(-25, -i * y_unit / 10 + Font.GetHeight() - 20);
            //    g1.DrawString((i).ToString(), lblfont, lblbrush, p);
            //}

            //drawing y unit in different way
            int cf = 100 / 10;
            for (int i = 1; i <= 10; i++)
            {
                PointF p1 = new PointF(-25, (y_unit * -i) - 5);
                double f = sf;
                g1.DrawString((i * cf).ToString(), lblfont, lblbrush, p1);
            }

            g1.Restore(gs);

            //Writing Title
            Font font = new Font("Times New Roman", 20, FontStyle.Bold);
            Brush brush = Brushes.White;
            Point titlePoint = new Point(90, 20);
            g1.DrawString("Celsius Vs Fahrenheits", font, brush, titlePoint);
        }

        private void btnDrawingPoints_Click(object sender, EventArgs e)
        {
            float x_unit = xmax / 10;
            float y_unit = ymax / 10;

            int dx = bmap1.Width / 10;
            int dy = 9 * bmap1.Height / 10;
            Matrix matrix2 = new Matrix(1, 0, 0, 1, dx, dy);
            g1.Transform = matrix2;

            float drawx = 0;
            float drawy = 0;

            for (int i = 1; i <= 10; i++)
            {
                drawx = ((i * x_unit) - 32 * (5 / 9)) - 3;
                drawy = (-i * y_unit) - 3;
                PointF p2 = new PointF(drawx, drawy);
                g1.FillEllipse(Brushes.Yellow, p2.X, p2.Y, 6, 6);
            }
            pictureBox1.Image = bmap1;
        }
    }
}
//to find out how many pixels in a degree.
//1* F = xmax/212
//xUnit = xmax/10

//Lab Assignment
//Draw the y_units (10 units on the y-axis)
//
//Title the picture-->
//Draw font "Celsius Vs Fahrenheit" at the center of pictureBox
//Write every point's value.

No comments:

Post a Comment