DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Predicting Ad Viewability With XGBoost Regressor Algorithm
  • Floyd's Cycle Algorithm for Fraud Detection in Java Systems
  • Metal and the Simulated Annealing Algorithm
  • Balancing Security and UX With Iterative Experimentation

Trending

  • Introducing Graph Concepts in Java With Eclipse JNoSQL
  • The Evolution of Scalable and Resilient Container Infrastructure
  • Supervised Fine-Tuning (SFT) on VLMs: From Pre-trained Checkpoints To Tuned Models
  • Modern Test Automation With AI (LLM) and Playwright MCP
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Draw, Plot 2d Line In C# (csharp) - Bresenham's Line Algorithm

Draw, Plot 2d Line In C# (csharp) - Bresenham's Line Algorithm

By 
Snippets Manager user avatar
Snippets Manager
·
Apr. 26, 07 · Code Snippet
Likes (0)
Comment
Save
Tweet
Share
3.8K Views

Join the DZone community and get the full member experience.

Join For Free
based on wikipedia

    public interface ISetPixel
    {
        void SetPixel(Point point);
    }
    public partial class Algorithms2D
    {
        public delegate void SetPixel(Point point);
        public static void Line(Point p0,Point p1,G plot)
            where G:ISetPixel
        {
            int x0=p0.X;
            int y0=p0.Y;
            int x1=p1.X;
            int y1=p1.Y;
            bool steep=abs(y1-y0)>abs(x1-x0);
            if (steep)
            {
                swap(ref x0,ref y0);
                swap(ref x1,ref y1);
            }
            if (x0>x1)
            {
                swap(ref x0,ref x1);
                swap(ref y0,ref y1);
            }
            int deltax=x1-x0;
            int deltay=abs(y1-y0);
            int error=-deltax/2;
            int ystep;
            int y=y0;
            if (y00)
                {
                    y=y+ystep;
                    error=error-deltax;
                }
            }
        }
        struct CSetPixel:ISetPixel
        {
            public CSetPixel(SetPixel setPixel)
            {
                this.setPixel=setPixel;
            }
            SetPixel setPixel;
            #region ISetPixel Members
            public void SetPixel(Point point)
            {
                setPixel(point);
            }
            #endregion
        }
        public static void Line(Point p0,Point p1,SetPixel plot)
        {
            Line(p0,p1,new CSetPixel(plot));
        }
        private static int abs(int p)
        {
            return Math.Abs(p);
        }
        private static void swap(ref T x0,ref T y0)
        {
            T z=x0;
            x0=y0;
            y0=z;
        }
    }


unit tests (c# 3.0):


    [TestFixture]
    public class Line
    {
        [Test]
        public void LineDiagonal()
        {
            List l = new List();
            Algorithms2D.Line(new Point(0,0),new Point(3,3),z=>l.Add(z));
            Assert.AreEqual(3, l.Count);
            Assert.AreEqual(new Point(0, 0), l[0]);
            Assert.AreEqual(new Point(1, 1), l[1]);
            Assert.AreEqual(new Point(2, 2), l[2]);
        }
        [Test]
        public void Line45()
        {
            List l = new List();
            Algorithms2D.Line(new Point(0, 0),new Point(6, 3), z => l.Add(z));
            Assert.AreEqual(6, l.Count);
            Assert.AreEqual(new Point(0, 0), l[0]);
            Assert.AreEqual(new Point(1, 0), l[1]);
            Assert.AreEqual(new Point(2, 1), l[2]);
            Assert.AreEqual(new Point(3, 1), l[3]);
            Assert.AreEqual(new Point(4, 2), l[4]);
            Assert.AreEqual(new Point(5, 2), l[5]);
        }
    }
Algorithm

Opinions expressed by DZone contributors are their own.

Related

  • Predicting Ad Viewability With XGBoost Regressor Algorithm
  • Floyd's Cycle Algorithm for Fraud Detection in Java Systems
  • Metal and the Simulated Annealing Algorithm
  • Balancing Security and UX With Iterative Experimentation

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!