Create a a code in C#

Ella McGillivray 26 Reputation points
2020-12-11T19:58:14.463+00:00

A line connects two points. It is a basic element in graphics. To draw a line, you need two points between which you can draw a line.
Digital Differential Analyzer (DDA) algorithm is the simple line generation algorithm which is explained step by step here.
Step 1 − Get the input of two end points (X1,Y1) and (X2,Y2)

Step 2 − Calculate the difference between the x components(dx) and the y components(dy) of the end points.

Step 3 − Based on the calculated difference in Step-2, you need to identify the number of steps to put pixels. If dx > dy, then you need more steps in x coordinate; otherwise in y coordinate.

Step 4 − Calculate the increment needed for the x coordinate and the y coordinate.

Step 5 − Put the pixels by successfully incrementing x and y coordinates accordingly and complete the drawing of the line.

Task: Create a line class, the line class should use the DDA algorithm to create the line.

Line Class Schematics

Constructor

Constructor 1 should accept 2 Point instances.

Constructor 2 should accept no points.

Constructor 3 should accept 4 integers corresponding to the two coordinates

Private Fields

_p0 Point

_p1 Point

_slope Real

_midpoint Point

Public Properites – (don’t use auto properties)

P0 (get & set) – the start point of the line

P1 (get & set) – the end point of the line

Slope (get only) – calculate & returns the slope of the line

Midpoint (get only) – calculate & returns the midpoint of the line

Public Methods
Draw() – draws the line.

Perturb(xmin, xmax, ymin ymax) – randomly moves the endpoints within the range of the parameters.

Collapse() – collapses the line to its mid-point

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,417 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Ella McGillivray 26 Reputation points
    2020-12-14T16:07:48.727+00:00

    Sorry about the delay

    namespace LinePoint {
    class Program {
    static void Main(string[] args) {
    #region Line/ Point
    Point p1 = new Point(2, 4);
    Point p2 = new Point(5, 7);

            p1.Draw();
            p2.Draw();
    
            Line line1 = new Line(p1, p2);
            line1.Collapse();
            //Point midPoint();
            #endregion
        }//end main
    }//end class
    

    }//end namespace

    namespace LinePoint
    {
    class Line {

        private Point _p0; //point 1
        private Point _p1; //point 2 
        private double _slope; // research
        private Point _midPoint; //point 
    
        #region Constructor 
        public Line(Point newP0, Point newP1) {
            _p0 = newP0;
            _p1 = newP1;
        }// end constructor1 
        public Line(int x1, int y1, int x2, int y2) {
            P0.X = x1;
            P0.Y = y1;
            P1.X = x2;
            P1.Y = y2;
        }//end constructor3
        #endregion
        #region Property
        public Point P0 {
            get { return _p0; }
            set { _p0 = value; }
        }//end property 
        public Point P1 {
            get { return _p1; }
            set { _p1 = value; }
        }//end property
        public double Slope { // get only 
            get { _slope = (P1.Y - P0.Y) / (P1.X - P0.X); return _slope; }
        }//end property
        public Point Midpoint { // get only
            get {
                _midPoint.X = (P0.X + P1.X) / 2;
                _midPoint.Y = (P0.Y + P1.Y) / 2;
                return _midPoint;
            }
        }//end property 
        #endregion
        #region Methods
    
        public void Draw() {
    
        }
    
        public void Perturb(int xmin, int xmax, int ymin, int ymax) {
            Random rand = new Random();
    
            int xEnd = rand.Next(xmin, xmax);
            int yEnd = rand.Next(ymin, ymax);
    
            if (xEnd > yEnd) {
                P0.Y += xEnd;
            } else {
                P0.X += yEnd;
            }//end if/else
        }//end perturb 
        public void Collapse() {
            int dx = P0.X - P1.X;
            int dy = P0.Y - P1.Y;
            int step;
            int locX;
            int locY;
            if (Math.Abs(dx) >= Math.Abs(dy)) {
                step = Math.Abs(dx);
            } else {
                step = Math.Abs(dy);
            }//end if/
            //Assign Values
            dx = dx / step;
            dy = dy / step;
            //Assigning Locations
    
            locX = P0.X;
            locY = P0.Y;
            locX = P1.X;
            locY = P1.Y;
        }//end draw method 
    
    
        #endregion
    
    }//end class
    }//end namespace
    

    namespace LinePoint {
    class Point {
    //fields
    private int _x = 0;
    private int _y = 0;

        //property
        public int X {
            get { return _x; }
            set { _x = value; }
        }//end property
        public int Y {
            get { return _y; }
            set { _y = value; }
        }//end property 
    
        //constructor
        public Point(int start_x = 0, int start_y = 0) {
            X = start_x;
            Y = start_y;
        }//end constructor
    
        public void Draw() {
            Console.SetCursorPosition(X, Y);
            Console.Write("+");
        }
    }
    

    }

    1 person found this answer helpful.
    0 comments No comments