Wednesday, 26 June 2013

Liang – Barsky Polygon Clipping and flood filling

Polygon Clipping is a complex operation that requires an outer polygon and an inner polygon. The outer polygon will usually be a rectangle which simplifies operations a bit. One of the most common implementations of Polygon Clipping is the Liang – Barsky algorithm and here you can find one implementation in Java. After clipping, a filling algorithm can be used to fill the polygon. The most common algorithm is the flood filling algorithm and here (method  floodFill(int row, int col, Color oldColor, Color newColor)) is an implementation in Java. Note that in order to fill an arbitrary polygon you will have to improve the method by testing if a given lies within the polygon.



Tuesday, 25 June 2013

The simplest way to iterate a linked list

The easiest way to iterate through a linked list in C or Java is with a for loop. The code is as simple as this:

C:

typedef struct _nd {
  struct _nd * next;
  char val[10];
} Node;

Node * i;
for (i=root; i; i=i->next) {
  printf("%s", i->val);
}

Java:

class Node {
  Node next;
  String val;
}

for (Node i=root; i!=null; i=i->next) {
  System.out.println(i.val);
}


Monday, 24 June 2013

Segmentation faults, valgrind and pointers

If you have segmentation faults in C the fastest way to fix them is by using valgrind. First compile the program with the -g parameters e.g. gcc -g main.c -o main. The -g adds debug data so you know exactly in which line you've the segmentation fault as soon as the valgrind runs. Then run the program with valgrind. valgrind ./main parameter1 parameter2. That's it, now valgrind will show you exactly where the segmentation fault occurred and it will be easy to identify why.


Friday, 21 June 2013

Bresenham's algorithm - Drawing on every octant

Drawing on every octant. We can see in Wikipedia a few implementations of Bresenham's algorithm  that allow us to draw lines on a single octant. Their implementations are efficient including the very fast integer-only implementation. The only problem is that lines can occur on all 8 octants.
By combining wikipedia's wisdom with this article we can draw efficiently on each of the 8 octants.

Thursday, 20 June 2013

An unofficial blog. Here you can ask questions and get answers for programming related topics for languages like C,C++,Java,OpenGL etc.