Giter Site home page Giter Site logo

line's Introduction

2D Line Drawing Samples

This project contains C source code for drawing 2D lines by different techniques.

All samples output PNGs with svpng.

License: public domain.

Bresenham

The fundamental line drawing algorithm by Bresenham [1], based on rosetta code.

line_bresenham.c

Sampling

Represent lines as capsule shapes, in order to support real-valued coordinates and variable width.

line_sampling.c

Anti-aliasing by super sampling

Simple anti-aliasing with 5x5 super sampling.

line_supersampling.c

Anti-aliasing by signed distnace field

Using signed distnace field (SDF) of capsule shape to perform anti-aliasing with single sample per pixel.

line_sdf.c

SDF optimization with AABB

Use AABB of capsule to reduce the number of samples. Render shapes into the buffer individually with alpha blending.

line_sdfaabb.c

Visual comparison

Performance comparison

$ make test
gcc -Wall -O3 -o line_bresenham line_bresenham.c
gcc -Wall -O3 -o line_sampling line_sampling.c
gcc -Wall -O3 -o line_supersampling line_supersampling.c
gcc -Wall -O3 -o line_sdf line_sdf.c
gcc -Wall -O3 -o line_sdfaabb line_sdfaabb.c
time ./line_bresenham
        0.03 real         0.02 user         0.00 sys
time ./line_sampling
        1.93 real         1.91 user         0.00 sys
time ./line_supersampling
       47.06 real        46.85 user         0.10 sys
time ./line_sdf
        2.00 real         1.98 user         0.00 sys
time ./line_sdfaabb
        0.03 real         0.03 user         0.00 sys

Final notes

  1. These samples are only for illustrative purposes.
  2. Line clipping are not performed.
  3. The SDF AABB sample can be further optimized by tigther quaduraliteral bounds.

References

[1] Bresenham, Jack E. "Algorithm for computer control of a digital plotter." IBM Systems journal 4.1 (1965): 25-30.

line's People

Contributors

miloyip avatar

Watchers

skyformat99 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.