C++

How to Draw a Point in C++ / SDL

Drawing a point on the screen is the first function that one may search in graphics programming. Plotting a pixel is the basic unit of all other graphical objects we see in everyday computing. In this article, we will take a look at how it is done using SDL's surface buffer.

Assuming you've already configured your SDL "includes" and 'libraries", we start the program by initializing the SDL libraries. Initializing SDL Video is the first step before using any of its graphic functions. You do this by:

Tags:

How to Draw a Circle (Midpoint and Bresenham's Algorithm)

With trigonometry functions such as sine and cosine, it is very easy to compute for the value of x and y for any given radius and teta.

We've all learned this from highschool and college class and it will not be tacked in this article. However, drawing a circle using these functions requires the usage of floating-point numbers which is slow, not to mention the typecasting from floating-point to integers as plotting a pixel requires such datatype.

How to Draw a Line (Bresenham's Algorithm)

The line equation represented by y = mx + b, is one of the most basic formulas in plotting a line. Where m represents the slope, x represents the value in horizontal coordinate and b represents the offset from y.

Suppose we have a line segment (x1, y1) to (x2, y2), the slope can be easily computed as (y2 - y1) / (x2 - x1) or to understand it better, we can read it as an increment in y for every step in x.

How to Represent Float Increments as Integer

If you've always hated incrementing (or decrementing) float variables by some constant float within the range of -1 0 1, and increment 0, and wanted to implement a better approach by using integral data types (such as char, short, int, and long), then this article best describes the solution to your ordeal. Applications of this optimization vary from different fields of computer programming. One of which is Bersenham's Algorithm for drawing a line.

How to Implement Image Scale and Crop

If you've ever worked with Adobe Photoshop or any image processing tool, you've probably resized an image to a certain frame or dimension. Suppose the original image dimension is 600x800 (3:4 aspect ratio) and you want to fit everything in a 500x750 (2:3 aspect ratio) frame. What you will do is to shrink the width from 600 to 500 and stretch the height from 800 to 750. This process of resizing is called image scaling in computer graphics.

Tags:

How to Create the Noise Effect

Noise is another special effect to emulate an old TV without a signal. A naive approach to do this graphic effect is to create a double for-loop and randomize the color of the pixel being placed on each of the coordinates of the screen.

for (y = 0; y < MAX_SCREEN_HEIGHT; ++y)
    for (x = 0; x < MAX_SCREEN_WIDTH; ++x)
        putPixel(x, y, RBGA(rand()%256, rand()%256, rand()%256, rand()%256));

This will work but not as fast as we would like it to be due to the following reasons:

Tags:

GOTO is Not Pure Evil

From garbage collection, hard to debug code, infinite loops, and other notorious bugs, there is quite a list of issues in using the GOTO statement. Due to GOTO's bad reputation, it's hardly being used and most programmers do not even know that such a control statement exists.

Let's take a look at the most common issues of GOTO statements and see if these assumptions are true. Otherwise, your idea that "GOTO is BAD" is just indoctrination!

How to Find the Sum of 1 to 100, and Its Variants

Legend has it that the German Mathematician, Carl Friedrich Gauss, who was an elementary student during the late 1700s, showed his teacher how quickly he summed up the numbers 1 to 100 by using a simple mathematical trick.

Centuries later, most programmers are still doing it the long way!

What is Brute Force Modeling?

Brute Force Modeling is a technique used to solve problems by tabulating results in hopes of finding a pattern by induction. The pattern or formula is then normally used as the basis for solving the problem thru iteration or recursion.

Let's take the Josephus Problem, where he (Josephus) and 40 other soldiers decided to choose mass suicide over capture by arranging themselves in a circle and killing the kth person in the circle. The problem of Josephus is where to sit in the circle such that he is the last to die (or escape).

Lattice Paths Part 3 (Combinations)

Starting in the top left corner of a 2 x 2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.

How many such routes are there through a 20 x 20 grid, or any N x N grid?

--

This is the last of the three-part series article about explaining how you could have arrived at the solution yourself in solving the Lattice Paths Problem. If you haven't tried it yet, do it first. I do not intend to deny you of your epiphany moment!

Note: All implementations are written in C/C++.

--

Subscribe to RSS - C++