Skip to content

Instantly share code, notes, and snippets.

View esmitt's full-sized avatar
🐢
As fast as a Pentium I

esmitt esmitt

🐢
As fast as a Pentium I
View GitHub Profile
@esmitt
esmitt / renderPolygon.cpp
Created December 6, 2017 17:08
Fill a polygon using scan line algorithm
// Store an horizontal line (x1, y) --> (x2, y)
void line(int x1, int x2, int y)
{
Color c;
c.r = lastColor.r;
c.g = lastColor.g;
c.b = lastColor.b;
for (int x = x1; x <= x2; x++)
{
Vertex v;
@esmitt
esmitt / plot3DPoints.m
Created December 7, 2017 13:32
Script to display a simple file with 3D points x y z
%output2.txt is the file to read
fid = fopen('output2.txt','rt');
indata = textscan(fid, '%f %f %f', 'HeaderLines', 2);
fclose(fid);
cmp = jet(numel(indata{3})); %is the same using X or Y
scatter3(indata{1}, indata{2}, indata{3}, [], cmp);
@esmitt
esmitt / antiAliasingLineOpenGL.cpp
Last active December 7, 2017 13:43
Some functions to draw a line using mid-point algorithm (bresenham) with anti-aliasing (average)
// Some types for easier vertex/color manipulation
struct Vertex
{
GLfloat x;
GLfloat y;
};
struct Color
{
GLfloat r;
@esmitt
esmitt / TeapotInTable.cpp
Created December 8, 2017 10:35
A single GLUT code to draw a teapot over a table
#include <stdlib.h>
#include <GL/glut.h>
#include <gl/GLU.h>
#include <iostream>
using namespace std;
GLUquadric* qobj;
GLUquadricObj *quadric;
void init(void)
@esmitt
esmitt / RemoveRecursive.cl
Created December 11, 2017 16:27
A LISP program to remove an element x from a list l
(DEFUN my-recursive-remove (x l)
(COND
((NULL l) NIL)
((NOT (ATOM (CAR l)))
(CONS (my-recursive-remove x (CAR l)) (my-recursive-remove x (CDR l)))
)
((AND (ATOM x) (EQ (CAR l) x)) (my-recursive-remove x (CDR l)))
(T (CONS (CAR l) (my-recursive-remove x (CDR l))))
)
)
@esmitt
esmitt / Insertion&Selection.cpp
Created December 11, 2017 16:40
Insertion & Selection sort step by step (printed by console) in C++
#include <iostream>
#include <math.h>
using namespace std;
const int LENGTH = 100;
//const int LENGTH = 100;
//const int LENGTH = 10;
//int A [LENGTH] = { 3, 9, 10, 2, 18, 29, 1, 9, 11, 20 };
int A[LENGTH] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
@esmitt
esmitt / LCS.cpp
Created December 20, 2017 09:40
Longest Increasing Subsequence for the problem on SPOJ (http://www.spoj.com/problems/XMEN/)
// http://www.spoj.com/problems/XMEN/
#include <algorithm>
#include <cstdio>
#include <stack>
#include <iostream>
using namespace std;
#define MAX_N 100005
@esmitt
esmitt / draw2DTextInVTK.cpp
Created March 8, 2018 17:09
Given a pair of points, this function write a text in the middle of points. Points are defined using glm
/// start, end are the ending point to write strLabel in the middle using VTK
auto fcnDrawLabel = [](vtkSmartPointer<vtkRenderWindow> renderWindow, glm::dvec3 start, glm::dvec3 end, std::string strLabel) -> vtkSmartPointer<vtkTextActor> {
vtkSmartPointer<vtkCoordinate> coordinate = vtkSmartPointer<vtkCoordinate>::New();
coordinate->SetCoordinateSystemToWorld();
coordinate->SetValue((start.x + end.x) / 2.0, (start.y + end.y) / 2.0, (start.z + end.z) / 2.0);
auto c = coordinate->GetComputedDisplayValue(renderWindow->GetRenderers()->GetFirstRenderer());
vtkSmartPointer<vtkTextActor> textActor = vtkSmartPointer<vtkTextActor>::New();
textActor->SetInput(strLabel.c_str());
@esmitt
esmitt / matrix4x4f.hpp
Created March 13, 2018 16:02
A class to handle a matrix 4x4 of float based on C code to rotate, transpose and multiply the same kind
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
class CMatrix4x4f
{
public: // attributes
@esmitt
esmitt / importImageVTK.cpp
Created October 24, 2018 15:02
A way to import data into a vtkImage from a data pointer (char*, float*, etc). The data pointer (pVolume) might be from external/auto-generated source.
#include <vtkImageImport.h>
// assume that dimensions contains the size of the volume (or image)
vtkNew<vtkImageImport> image_import;
image_import->SetDataSpacing(0.76, 0.76, 0.5); //x, y, z
image_import->SetDataOrigin(0, 0, 0);
image_import->SetWholeExtent(0, (int)dimensions[0] - 1, 0, (int)dimensions[1] - 1, 0, (int)dimensions[2] - 1);
image_import->SetDataExtentToWholeExtent();
image_import->SetDataScalarTypeToFloat(); //float, or change to any datatype
image_import->SetNumberOfScalarComponents(1);