How to rotate an image in C#

  1. Calculate SIN and COS once
  2. Apply these in a formula to all pixels
  3. Do this in reverse to avoid gaps in the final image

Rotation-matrix

To rotate a pixel around an axis, we need to do some sinus and co-sinus calculation to it because, in short, rotation follows a circular path.

The good part is that the sinus and co-sinus part of the calculation remains the same for every pixel, because they all rotate the same amount.

The next code sets up the values needed later in the main loop. And C#’s math functions want to see Radians, not degrees.

double rotationRadians = userRotate * (Math.PI / 180);

float sin = (float) Math.Cos(rotationRadians);
float cos = (float) Math.Sin(rotationRadians);

In reverse

If we where to go over every pixel in the original image and calculate it’s new position, then there would be gaps in the destination image.

Therefore, we go over every pixel in the destination image, and calculate back where its original position is, that way we always have a pixel color when within the image’s border.

Continue reading “How to rotate an image in C#”

With A Resistor-divider You Can AnalogRead() Anything!

Voltage to high or to low to analogRead() directly into your microcontroller? With the right resistor-divider, we can read anything we want.

There are three distinct problems, each with its own solution.

Table of Contents

The positive voltage is above 3.3v or 5v

If we apply double our maximum voltage over two identical resistors in series, we expect to measure half our voltage at the midpoint. It is this midpoint we are going to measure and feed to analogRead().

Simple resistor-divider, voltage at midpoint is half the voltage at the top
Simulation of the voltages in a resistor-network, all the voltages are evenly scaled
Continue reading “With A Resistor-divider You Can AnalogRead() Anything!”

How to drive a relay from a digital pin, two solutions

The first is by using an NPN-transistor, and the second a N-channel-MOSFET. Both circuits are pretty much the same otherwise.

NPN transistor

Using a transistor is the cheapest and fail proof way of doing this. Setting a output pin high on your CPU sends a resistor limited current to the base of the transistor.

A multiple of that current is allowed through through the transistor and therefore the relay-coil, powered from the same voltage source as the CPU, but not through it.

Use a NPN transistor to drive a relay from the low side with little current going though the IO pin
Continue reading “How to drive a relay from a digital pin, two solutions”

How to create a power limiter to get zero-on-the-meter to save taxes

If you are one of the many people exporting electricity to the grid, chances are you are familiar with the following…

When you export electricity you also get to pay energy taxes and or other grid fees. So even if your net-consumption is zero, you still own taxes. This might not be the case in your country or your specific utilities provider, but it will be the future for most of us.

With solar you are either importing or exporting power

The graph above is a simplification of typical grid usage for a solar house over the course of 24 hours. At night we consume power from the grid, and at daytime we are exporting. Either way there is power crossing the meter which can potentially cost us money as taxes.

Continue reading “How to create a power limiter to get zero-on-the-meter to save taxes”