So you needed to draw a grid … or a ruler … or another x-y-ish vector graphics and you ended up with code snippets like this everywhere:

public void Grid(Graphics g, Size size, int step) {
    for (int i = 0; i < size.Width; i+=step)
    {
        g.DrawLine(Pens.Black,
            new Point(i, 0),
            new Point(i, size.Height)
        );
    }
    for (int i = 0; i < _size.Height; i += step)
    {
        g.DrawLine(Pens.Black,
            new Point(0, i),
            new Point(size.Width, i)
        );
    }
}
You can avoid redundancy in such situations by using the strategy pattern. First create a new interface called IDirection.
public interface IDirection
{
    int GetMoving(Size size);
    int GetStatic(Size size);
    void AddMoving(ref int x, ref int y, int step);
    Point GetPoint(int moving, int static_);
}
Now imagine yourself free- falling into a deep hole. Remember, you are a programmer. So stop screaming and start thinking about the fall. Your moving direction is vertical. And since you are in a vertical free- fall you almost don’t move in x direction. Therefore your static direction is horizontal. Now write this in code.
public class VerticalDirection : IDirection
{
    public int GetMoving(Size size)
    {
        return size.Height;
    }

    public void AddMoving(ref int x, ref int y, int step)
    {
        y += step;
    }

    public Point GetPoint(int moving, int fixed_)
    {
        return new Point(fixed_, moving);
    }

    public int GetStatic(Size size)
    {
        return size.Width;
    }
}
And a HorizontalDirection class.
class HorizontalDirection : IDirection
{
    public int GetMoving(System.Drawing.Size size)
    {
        return size.Width;
    }

    public void AddMoving(ref int x, ref int y, int step)
    {
        x += step;
    }


    public Point GetPoint(int moving, int fixed_)
    {
        return new Point(moving, fixed_);
    }

    public int GetStatic(Size size)
    {
        return size.Height;
    }
}
There we go. Static and moving direction for each concrete direction class are encapsulated and we can now write new grid drawing code.
public void Grid(Graphics g, Size size, int step)
{
    Grid(g, size, step, new VerticalDirection());
    Grid(g, size, step, new HorizontalDirection());
}

public void Grid(Graphics g, Size size, int step, IDirection direction)
{
    for (int i = 0; i < direction.GetMoving(size); i += step)
    {
        g.DrawLine(Pens.Black,
            direction.GetPoint(i, 0),
            direction.GetPoint(i, direction.GetStatic(size))
        );
    }
}

Sometimes you want to know index of the minimal value of an array. I’ve frequently seen programmers using two data structures to accomplish this task: one to hold the index and the other to hold the value (i.e. current minimum) while iterating. Unless you need to optimize your code for speed you really only need an integer for this task.

int find_min(int* vals, int len)
{
    int mindx= 0;
    for (int i = 1; i < len; i++)
        if (vals[i] < vals[mindx]) mindx= i;
    return mindx;
}

I've been searching for this for three years and now my collection is complete.

GNU Make is a decent tool. But due to the fact that I commonly create only a handful of Makefiles per project and that its syntax is easy to forget (or -perhaps- hard to remember) - it often takes me an hour before I create a new generic Makefile for a project.

More often then not I end up analysing and recycling an existing one from one of my previous projects. Thus I wrote a generic Windows starter that will work on any folder that contains .c and .h files as long as you have The GNU C Compiler Suite and Make installed on your machine.

Simply add this Makefile to your folder and change the target (the default is "buddy.exe") to the name of your desired exe output file. Make sure that one .c file contains the main function and the rest will be done by the build system.

The main tricks being used are:

1. Obtaining list of all *.c files in folder...

SRCS    =   $(wildcard *.c)
2. ...using it to generate all object files.
OBJS    =   $(patsubst %.c,%.o,$(SRCS))
3. Creating dependency file named .depend by using -MM switch on gcc (i.e. fake compilation step).
$(CC) $(CFLAGS) $(SRCS) -MM >> .depend
4. Including dependency file into makefile using -include (minus means it will not complain if the file is not there i.e. on the first run).
-include .depend
5. And last but not least: redirection of stderr to NUL to prevent DEL command from complaining when there are no matching files.
del *.o 2>NUL
Here is the entire Makefile.


CC = gcc CFLAGS = -I. SRCS = $(wildcard *.c) OBJS = $(patsubst %.c,%.o,$(SRCS)) .PHONY: all clean all: depend buddy.exe depend: $(SRCS) del .depend 2>NUL $(CC) $(CFLAGS) $(SRCS) -MM >> .depend -include .depend %.o: %.c $(CC) -c -o $@ $< $(CFLAGS) buddy.exe: $(OBJS) $(CC) -o $@ $^ clean: del *.o 2>NUL del *.exe 2>NUL del .depend 2>NUL

Following step-by-step instructions by Adam J. Kunen I was able to compile my own gnu arm tool-chain on Ubuntu Linux 12.10.

I used following packages:

  • binutils-2.23 
  • gcc-4.8.1 (Adam also recommends downloading packages gcc-core and g++ but I skipped those)
  • gdb-7.6 
  • newlib-2.0.0 
The compilation of gcc-4.8.1 initially failed with the "Link tests are not allowed after GCC_NO_EXECUTABLES" message so I added the flag --with-system-zlib like this

../../src/gcc-4.8.1/configure --target=arm-none-eabi --prefix=$MYTOOLS --enable-interwork --enable-multilib --enable-languages="c,c++" --with-newlib --with-headers=../../src/newlib-2.0.0/newlib/libc/include --with-system-zlib

UPDATE: Unfortunately this fix has some issues. It will not build libgcc. Seeking for a better solution.

Newer Posts Older Posts Home

Blogger Syntax Highliter