وبلاگ رسمی علیرضا شهامت در بلاگ اسکای

وبلاگ رسمی علیرضا شهامت در بلاگ اسکای

وبلاگ رسمی علیرضا شهامت در بلاگ اسکای - تأسیس شده در سال 1396
وبلاگ رسمی علیرضا شهامت در بلاگ اسکای

وبلاگ رسمی علیرضا شهامت در بلاگ اسکای

وبلاگ رسمی علیرضا شهامت در بلاگ اسکای - تأسیس شده در سال 1396

سی شارپ - مقاطع مخروطی - سهمی و خط هادی و کانون آن



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;

namespace BVH
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }





        Bitmap bmp;

        int bmp_width, bmp_height;

        Color my_color;

        int alpha, r, g, b;

        int x_old, y_old;

        float x_new, y_new;



        F_Point Kaanun_F1, Kaanun_F2;

        F_Point yek_noghte_ruye_khatte_haadi_A;
        F_Point yek_noghte_ruye_khatte_haadi_B;

        F_Point yek_noghte_ruye_safhe_M;

        F_Point noghte_ye_taaamod_H;

        float m_shibe_AB;





        float d_khatte_haadi_a;
        float d_khatte_haadi_b;
        float d_khatte_haadi_c;

        float a1, b1, c1;

        float a2, b2, c2;

        F_Point A, B, M, H, F1, F2;

        float m1, m2;

        float MH, MF1, MF2;




        private void button_Draw_01_Click(object sender, EventArgs e)
        {



            Graphics.FromHwnd(pictureBox1.Handle).
                Clear(Color.Black);


            bmp_width = pictureBox1.Width;
            bmp_height = pictureBox1.Height;




            bmp = new Bitmap(bmp_width, bmp_height);




            A = new F_Point(1f, 5f);
            B = new F_Point(3f, 4f);



            F1 = new F_Point(-3f, -2f);

            F2 = new F_Point(-6f, -3f);


            m1 = shibe_khatte(A, B);




            for (x_old = 0; x_old < bmp_width; x_old++)
            {

                for (y_old = 0; y_old < bmp_height; y_old++)
                {


                    x_new = ((float)x_old - (float)pictureBox1.Width / 2f) / 20f;

                    y_new = -((float)y_old - (float)pictureBox1.Height / 2f) / 20f;

                    M = new F_Point(x_new, y_new);


                    a1 = -m1;

                    c1 = m1 * A.x - A.y;

                    m2 = (-1) / m1;

                    a2 = -m2;

                    c2 = m2 * M.x - M.y;


                    H = new F_Point(
                        (c2 - c1) / (a1 - a2)
                        ,
                        (a2 * c1 - a1 * c2) / (a1 - a2)
                        );
                    ;


                    MH = distance(M, H);

                    MF1 = distance(M, F1);


                    alpha = 255;



                    //if (y_new < Math.Pow(x_new, 2))
                    //{
                    //    r = 32;
                    //}
                    //else
                    //{
                    //    r = 255;
                    //}


                    ///////////////////////////////////////////////////////

                    // Red 1

                    //if (MF1 < 12f)
                    //{

                    //    if (MF1 < 0.25f)
                    //    {
                    //        r = 255;
                    //    }
                    //    else
                    //    {
                    //        int r2;
                    //        r2 = (int)(MF1 * 20);

                    //        if (r2 > 255)
                    //            r = 255;
                    //        else if (r2 < 0)
                    //            r = 0;
                    //        else
                    //            r = r2;

                    //    }

                    //}
                    //else
                    //{
                    //    r = 0;
                    //}

                    ////////////////////////////////////////////////////////////////////


                    // Red 2:

                    float red_arg;

                    red_arg = (float)(
                        Math.Pow(MF1, 1d) * 1f + Math.Pow(MH, 1d) * 1f
                        ) * 1;

                    if (F_between(-10.5f, red_arg, 10.5f))
                    {

                        int r2;

                        r2 = (int)(red_arg * 10f);

                        //r2 = Math.Abs(r2);


                        if (r2 < 0)
                            r = 0;
                        else if (r2 > 255)
                            r = 255;
                        else
                            r = r2;

                    }
                    else
                    {
                        r = 255;
                    }



                    /////////////////////////////////////////////////////////////////////







                    // Green

                    if (F_between(0f, MF1, 0.5f))
                    {
                        r = 255;
                        g = 255;
                    }
                    else if (MH < 8f)
                    {

                        if (MH < 0.25f)
                        {
                            g = 255;
                        }
                        else
                        {
                            int g2;
                            g2 = (int)(MH * 40);

                            if (g2 > 255)
                                g = 255;
                            else if (g2 < 0)
                                g = 0;
                            else
                                g = g2;

                        }

                    }
                    else
                    {
                        g = 80;
                    }


                    // g = 0;

                    //if (MH < 0.5f)
                    //{
                    //    g = 255;
                    //}
                    //else
                    //{
                    //    g = 0;
                    //}


                    //if (MF1 - MH < 0.5f)
                    //{
                    //    b = 255;
                    //}
                    //else
                    //{
                    //    b = 0;
                    //}


                    ////////////////////////////////////////////////////////////////

                    // Blue 1:

                    //if (MF1 - MH < 8f)
                    //{

                    //    if (MF1 - MH < 0.25f)
                    //    {
                    //        b = 255;
                    //    }
                    //    else
                    //    {
                    //        int b2;
                    //        b2 = (int)((MF1 - MH) * 50);

                    //        if (b2 > 255)
                    //            b = 255;
                    //        else if (b2 < 0)
                    //            b = 0;
                    //        else
                    //            b = b2;

                    //    }

                    //}
                    //else
                    //{
                    //    if (MF1 - MH < 2.25f)
                    //    {
                    //        b = 64;
                    //    }
                    //    else
                    //    {
                    //        int b2;
                    //        b2 = 255 - (int)((MF1 - MH) * 8.5f);

                    //        if (b2 > 255)
                    //            b = 255;
                    //        else if (b2 < 0)
                    //            b = 0;
                    //        else
                    //            b = b2;

                    //    }
                    //}


                    /////////////////////////////////////////////////////////

                    // Blue 2:



                    //if (F_between(0f, MF1 - MH, 0.5f))
                    //{

                    //    b = 255;

                    //}
                    //else if (F_between(-0.5f, MF1 - MH, 0f))
                    //{
                    //    b = 192;
                    //}


                    ///////////////////////////////////////////////////////////


                    /////////////////////////////////////////////////////////

                    // Blue 3:

                    if (F_between(-0.5f, MF1 - MH, 0.5f))
                        b = 255;
                    else if (F_between(-52f, MF1 - MH, 52f))
                    {

                        int b2;

                        //b2 = (int)(MF1 - MH);

                        b2 = (int)((MF1 - MH) * 12.5f);

                        b2 = Math.Abs(b2);


                        if (b2 < 0)
                            b = 0;
                        else if (b2 > 255)
                            b = 255;
                        else
                            b = b2;

                    }



                    ///////////////////////////////////////////////////////////

                    // b = 0;

                    my_color = Color.FromArgb(alpha, r, g, b);

                    bmp.SetPixel(x_old, y_old, my_color);
                }

            }



            Graphics.FromHwnd(pictureBox1.Handle).
                DrawImage(bmp, 0, 0);


        }



        public static float distance(F_Point A, F_Point B)
        {
            float AB;

            AB = (float)Math.Pow(
                    Math.Pow((A.x - B.x), 2) +
                    Math.Pow((A.y - B.y), 2)
                    , (1f / 2f)
                    );


            return AB;
        }




        public static float shibe_khatte(F_Point A, F_Point B)
        {
            float m;

            m = (A.y - B.y) /
                 (A.x - B.x);


            return m;
        }




        public static bool F_between(float lower_bound, float arg, float upper_bound)
        {

            if (lower_bound <= arg && arg <= upper_bound)
            {
                return true;
            }
            else
            {
                return false;
            }

        }



        private void Form1_SizeChanged(object sender, EventArgs e)
        {

            trackBar_width.Maximum = this.Width - 45;
            trackBar_height.Maximum = this.Height - 115;

        }

        private void trackBar_width_Scroll(object sender, EventArgs e)
        {
            pictureBox1.Width = trackBar_width.Value;
        }

        private void trackBar_height_Scroll(object sender, EventArgs e)
        {
            pictureBox1.Height = trackBar_height.Value;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            trackBar_width.Minimum = 50;
            trackBar_height.Minimum = 50;

            trackBar_width.Maximum = this.Width - 45;
            trackBar_height.Maximum = this.Height - 115;


        }



    }

    public class F_Point
    {


        public float x;

        public float y;

        public F_Point(float x_input, float y_input)
        {
            x = x_input;

            y = y_input;
        }




    }



}







لینک های دانلود در مطالب بعدی قرار دارند.