Implementing Line Drawing Algorithm

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In computer graphics, a line is a basic element that can be drawn on a screen. Drawing a line on a screen involves setting the corresponding bits in the screen's memory. In this challenge, we will implement a method that draws a line on a screen given its width and two absolute pixel positions.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) algorithm/BasicAlgorithmsGroup -.-> algorithm/bit_manipulation("`Bit Manipulation`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/ErrorandExceptionHandlingGroup -.-> python/raising_exceptions("`Raising Exceptions`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills algorithm/bit_manipulation -.-> lab-268813{{"`Implementing Line Drawing Algorithm`"}} python/variables_data_types -.-> lab-268813{{"`Implementing Line Drawing Algorithm`"}} python/numeric_types -.-> lab-268813{{"`Implementing Line Drawing Algorithm`"}} python/type_conversion -.-> lab-268813{{"`Implementing Line Drawing Algorithm`"}} python/conditional_statements -.-> lab-268813{{"`Implementing Line Drawing Algorithm`"}} python/for_loops -.-> lab-268813{{"`Implementing Line Drawing Algorithm`"}} python/lists -.-> lab-268813{{"`Implementing Line Drawing Algorithm`"}} python/tuples -.-> lab-268813{{"`Implementing Line Drawing Algorithm`"}} python/function_definition -.-> lab-268813{{"`Implementing Line Drawing Algorithm`"}} python/classes_objects -.-> lab-268813{{"`Implementing Line Drawing Algorithm`"}} python/encapsulation -.-> lab-268813{{"`Implementing Line Drawing Algorithm`"}} python/raising_exceptions -.-> lab-268813{{"`Implementing Line Drawing Algorithm`"}} python/build_in_functions -.-> lab-268813{{"`Implementing Line Drawing Algorithm`"}} end

Draw Line

Problem

Implement the method draw_line(screen, width, x1, x2) where screen is a list of bytes, width is divisible by 8, and x1, x2 are absolute pixel positions. The method should set the corresponding bits in screen to draw a line from x1 to x2.

Requirements

The implementation of draw_line must meet the following requirements:

  • The inputs may not be assumed to be valid.
  • The corresponding bits in screen must be set to draw the line.
  • It may be assumed that the implementation fits memory.

Example Usage

The following examples illustrate the expected behavior of draw_line:

  • Invalid inputs -> Exception
    • screen is empty
    • width = 0
    • any input param is None
    • x1 or x2 is out of bounds
  • General case for len(screen) = 20, width = 32:
    • x1 = 2, x2 = 6
      • screen[0] = int('00111110', base=2)
    • x1 = 68, x2 = 80
      • screen[8], int('00001111', base=2)
      • screen[9], int('11111111', base=2)
      • screen[10], int('10000000', base=2)

Summary

In this challenge, we implemented a method that draws a line on a screen given its width and two absolute pixel positions. The implementation met the requirements of handling invalid inputs, setting the corresponding bits in screen, and fitting memory.

Other Algorithm Tutorials you may like