• Unlikely Objects
  • Ludography
    • Nonoku
    • Acquisition
    • Image Mark Up
  • About
Menu

Unlikely Objects

  • Unlikely Objects
  • Ludography
  • Pint Sized Planet
    • Nonoku
    • Acquisition
    • Image Mark Up
  • About

An Evening of Code: Drawing a Line

February 9, 2015

Not hugely exciting but I did also spend a little time cleaning up an old project. Didn't end up doing as much as I thought was needed so instead I started looking at ways to start learning L-systems. To begin, I'll need a way to get them on the screen. For the first go I figured being able to draw lines to a texture would be sufficient. This is a quick and dirty implementation of Bresenham's line algorithm. I don't need it to be fast so substituted floats for the bit shifting just to make it easy. Nothing to be proud of here but it does get me what I need for tonight.

public void DrawLine(int x0, int y0, int x1, int y1, Color color) {
  int dy = y1 - y0;
  int dx = x1 - x0;
  float error = 0;
  float deltaError = 0;
  if (dx != 0 && dy != 0) {
    deltaError = Mathf.Abs((float)dy / (float)dx);
  }

  int y = y0;
  int x = x0;
  for (int i = 0; i < Mathf.Abs(dx); i++) {         
    texture2D.SetPixel(x, y, color);
    error = error + deltaError;
    while (error >= 0.5f) {
      texture2D.SetPixel (x, y, color);
      y += Mathf.RoundToInt(Mathf.Sign(dy));
      error = error - 1.0f;
    }
    x += Mathf.RoundToInt(Mathf.Sign(dx));
  }
}
In An Evening of Code, Unity Tags indiedev, procedural, gamedev, unity
← An Evening of Code: Koch SnowflakeLinkDump: L-Systems →

Latest

Unlikely Objects
Fractal Plant in Godot
about a year ago
Binary Tree in Godot
about a year ago
Sierpinski Triangle in Godot
about a year ago
Re-visiting some old code
about a year ago
React - Starting a New Project
about 3 years ago
Satsuma Tosser
about 4 years ago
1HGJ #002: Deeper and Deeper
about 7 years ago
1HGJ #001: Telekinetic Bomberman Rush
about 7 years ago
Migrating to NSPersistentContainer
about 7 years ago
Nonoku - SpriteKit Shader
about 7 years ago

Powered by Squarespace