Saturday, December 4, 2010

Btrace 2 - a gallery of early mistakes

Here are some of the missteps.


Btrace 1

I'm implementing a raytracer in Python for my computer graphics class. I'll be posting some of the results here. Here is one of the first renders of a plane with no illumination model. The aliasing is pretty bad. The checkerboard pattern is procedurally generated.


Monday, November 22, 2010

DIY Scatter plot screenshot

No real tip, but I'm doing Visual Analytics this semester at Tufts and I spent unnecessary amount of time just to get this far. This is a rendering of the Iris data set from UCI.

Wednesday, November 10, 2010

How to do modulus with floats and doubles in C

You may be tempted to use the '%' operator but that's only for integers.
To do modulus arithmetic with doubles and floats use the math.h library.
Use the following functions:


double fmod(double x, double y);
float fmodf(float x, float y);
long double fmodl(long double x, long double y);


Use -lm to link to the library.

Sunday, October 24, 2010

Emailing from the commandline with mutt


echo "Place message here: this is cool." | \
mutt -s "how to email from the commandline" \
-a with_mime_attachments.bz \
joe@example.org



Yes I know there's mail, but mail doesn't do MIME. :)

Monday, October 11, 2010

Cigar Guy at Ryder Cup (aka it's photoshop time!)



Borrowed off the internets. If this is your pic and you object to it being here, let me know!

Thursday, September 30, 2010

Why C Arrays are not really pointers

Ok. This is to settle an argument regarding C, arrays and pointers. I know we were all taught to treat arrays like pointers and they do behave like pointers for the -MOST- part. Try this code for a little mind experiment. Don't cheat! Think about the answer first:


#include <stdio.h>

int main()
{
int len = 100;
char myarr[len];
char *ptr;
ptr = myarr;

printf( "sizeof(myarr) = %d\n", sizeof(myarr) );
printf( "sizeof(ptr) = %d\n", sizeof(ptr) );
return 0;
}


What will this little program print? ;)
Stay tuned for the answer!

Added 24 October 2010:
Oops. I've been so busy I fogot to post a follow up. Anyway, you can easily verify what the answer will be by compiling and running.

Monday, September 13, 2010

Little Tech Tips, now live from Tufts University!


Hello true believers.

Or at least those of you who happen to drop by from Google search. :)
Little Tech Tips has been on hiatus for several months because of a big change. I've gone back to school so we are now blogging all things little, tech and what not from the most excellent Tufts University.

As such, we will be covering new topics (which will most conveniently be related to whatever class I'm taking/TAing. ;)

Stay tuned!

Wednesday, June 16, 2010

Sharing data with BackgroundWorker in C#/WPF

BackgroundWorker looked like a Good Thing, until I found that I couldn't pass it a BitmapSource because it "belonged to another thread." Even simple accesses to get the BitmapSource's state was not allowed.

After much stumbling and googling, I found that Freeze-ing an object allowed it to be shared between threads (which includes BackgroundWorker).

Problem solved!

Monday, May 17, 2010

Free Good Post-it like program for Windows




Stickies by Tom Revell is a good option. It's not open source but it is free.

It has a lot of features. Here's what I like about it.
1. Colors are available
2. You can set alarms
3. Free
4. Check boxes

The one thing I don't like about it is it is not open source, but that's not a deal breaker.

Thursday, May 13, 2010

WPF/XAML mapping to custom assemblies and clases in less than 60 seconds

You can map XML namespaces to assemblies using a series of tokens within an xmlns prefix declaration, similar to how the standard WPF and XAML-intrinsics XAML namespaces are mapped to prefixes.

The syntax takes the following possible named tokens and following values:

clr-namespace: The CLR namespace declared within the assembly that contains the public types to expose as elements.

assembly= The assembly that contains some or all of the referenced CLR namespace. This value is typically just the name of the assembly, not the path, and does not include the extension (such as .dll or .exe). The path to that assembly must be established as a project reference in the project file that contains the XAML you are trying to map. In order to incorporate versioning and strong-name signing, the assembly value can be a string as defined by AssemblyName, rather than the simple string name.

Note that the character separating the clr-namespace token from its value is a colon (:) whereas the character separating the assembly token from its value is an equals sign (=). The character to use between these two tokens is a semicolon. Also, do not include any whitespace anywhere in the declaration.




Got that?
Taken from here.

Wednesday, May 12, 2010

more MSDN WPF mind-bending docs

Adorners receive input events just like any other FrameworkElement. Because an adorner always has a higher z-order than the element it adorns, the adorner receives input events (such as Drop or MouseMove) that may be intended for the underlying adorned element. An adorner can listen for certain input events and pass these on to the underlying adorned element by re-raising the event.

At some point, the repetition of adorn makes my head spin.


adorn, adorn, adorn, adorn, adorn.

See?


See original documentation here.

Monday, May 10, 2010

WPF WTF on MSDN part 1

My mind went blank after reading the following:

For instance, the underlying attached event Mouse.MouseDown can more easily be handled on any given UIElement by using MouseDown on that UIElement rather than dealing with attached event syntax either in XAML or code. The attached event serves a purpose in the architecture because it allows for future expansion of input devices. The hypothetical device would only need to raise Mouse.MouseDown in order to simulate mouse input, and would not need to derive from Mouse to do so. However, this scenario involves code handling of the events, and XAML handling of the attached event is not relevant to this scenario.

from here.

Anyone care to explain this one?

Tuesday, April 6, 2010

How to setup Linux as a NAT router

I'm pretty sure this is all you need:

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -A FORWARD -i eth1 -j ACCEPT
echo 1 > /proc/sys/net/ipv4/ip_forward

Of course, you obviously have to have iptables installed for this to work. I got it working on my Arch Linux and my Opensuse 11.1.

Your mileage may vary. Please post comments for any suggestions or problems with this little hack.

Tuesday, March 30, 2010

How to convert multiple images (command line)

Using Imagemagick, it is very easy.

Say we have a bunch of bmp files we want to convert to png. This hack assumes a couple of things:
1. The picture files end with bmp. On Linux and other unixy OSes, this is important. Not so important on Windows.
2. The mogrify command alters the original file so you may want to back up your files before doing this. :)

So with the disclaimer behind us, here's the command:

mogrify -format png *.bmp


Edit:
So I'm not sure why mogrify is behaving this way, but I just tried in on my Opensuse 11.1 with Imagemagick version 6.4.3 and the files were not mogrified. Instead of overwriting the file, a new file with a png extension was created. Which makes sense now that I think about it. :)

Link-o-rama for March 30, 2010

1. Warner Bros recruiting students to spy on other kids for downloading.
2. Open standards in Europe under attack.
3. Gaiking trailer!
4. On the serious side, 9 alleged cyberbullies (and real life bullies) charged in court for causing young girl's suicide.

Tuesday, March 23, 2010

cmake - stopping the automake/autotools/autoconf insanity

In the process of researching an opensource library, I came across cmake. At this point, cmake looks very promising. I will be writing a series of tutorials on cmake but in the meantime, check out a tool (used by the brilliant people at KDE) written in Ruby that converts projects that use automake/autotools/autoconf to cmake.

Saturday, February 13, 2010

Mass rename/move in bash

for i in *.HTM;
do mv $i `basename $i HTM`html;
done


There are a number of components to this mini hack.

First is the for loop. This is pretty straightforward if you know bash path regular expressions. The regex *.HTM is expanded to make a list of all files in the current directory that end in HTM. Note that this is case sensitive in Linux. Then we itereate over this list.
The meat of the loop is in the do part. We mv the original name to a name constructed by the following expression:

`basename $i HTM`html

The backticks means run the command in the backticks and use the output.

basename does two things here. It strips away the directory part of the path. In this case, there is none. The important part is the HTM argument to basename which means that we want to strip away the HTM suffix. For example:

basename readme.txt txt

will return

readme.

Note the period is kept.
Then doing this:

`basename $i HTM`html

Will return the basenemt concatenated with html. This is how we do string concatenation in bash. See previous article: String concatenation in bash.

Put that all together, we are able to do a mass rename/move in bash. Note that if we want to do more complicated pattern matching and renaming, we probably would have to do it in Python or Perl.

Thursday, February 4, 2010

sed: find and replace in place

To replace in place means to change the original file with the substitutions. (Contrary to the default behavior of sed which is to print to standard out.)

It's simple:

sed -i 's/sad/happy/g' filetochange.txt

Wednesday, February 3, 2010

A click from the Office of the President

I was going through the logs for Little Tech Tips recently and found this:

office_of_the_president_and_IE6

Haven't they heard?

Of course it was kind of cool that someone at the Executive Office of the President uses vim. :)

C++ Tidbit: The Copy Constructor

The copy constructor is a constructor that takes a single paramter that is:
1. Usually const
2. A reference to an object of the same type. (Meaning the class that is being defined.)

The copy constructor is important in C++ for a number of reasons:

- It is used to initialize an object from another object of the same type. Everything other use of the copy constructor stems from this use.
- The copy constructor is used to copy an object to pass it as an argument to a function. That is, when an object is passed by value, the copy constructor is called.
- And of course, it is used to copy an object to return it from a function.
- It is also used initialize elements of a sequential container

The important thing to note is that if we don't define a copy constructor, the compiler will define one for us. The new object is initialized by copying the members of the old object one by one. For some cases this is actually what we want.

Wednesday, January 13, 2010

how to get the word count in vim



It's really simple, albeit a little difficult to remember:

In command mode, press g, then ctrl-g

This will actually give you a number of things.

It will give you:

- the current column
- the line number
- the word count
- the byte count

as such:

Col 22 of 88; Line 1 of 13; Word 4 of 119; Byte 22 of 813

So the word count of this buffer is 119. The other bit of information refers to where the cursor is when you issued the g / ctrl-g command.

Friday, January 1, 2010

Once more unto the breach dear friends...

The Little Tech Tips blog has been on holiday break, but will resume the regularly scheduled programming in a couple of days.

Thanks to all my readers, all 8 of you. ;)

A happy new year to all.