# Terrain Generation part 4: deriving temperature

Everything so far has been about creating interesting elevation, a height map. Visualizing that has been shades of green for the land and shades of blue for the water.

On my alien planet I’m working towards the procedural generation of multiple biomes may flourish. In order to derive these distinct biomes, I’m looking to add another aspect: temperature.

Temperature for my purposes is going to start with using latitude and elevation. Also ignoring seasonal differences for now. Further, anything at or below sea level is glossed over in regards to temperature differences.

Now being an alien planet, I can arrange mountain heights, elevation temperature loss, and everything else to my liking.

``````struct TemperatureFunction<T> where T: NoiseFn<f64, 2> {
sea_level: f64,
max_elevation_meters: f64,
celsius_loss_per_thousand_meters: f64,
map_height: f64,
source: T
}
impl<T> TemperatureFunction<T> where T: NoiseFn<f64, 2> {
fn new(source: T, map_height: f64) -> TemperatureFunction<T> {
TemperatureFunction {
sea_level: 0.5,
max_elevation_meters: 8000.0,
celsius_loss_per_thousand_meters: 5.0,
source,
map_height
}
}
}
impl<T> NoiseFn<f64, 2> for TemperatureFunction<T> where T: NoiseFn<f64, 2> {
fn get(&self, point: [f64; 2]) -> f64 {
let latitude = (point - self.map_height / 2.0).abs() / self.map_height * 0.9;
let base_temp_from_latitude = 35.0 - (latitude*60.0);
// Anything below 0.5 is below sea level.
let elevation_above_sea_level = self.source.get(point).max(self.sea_level) - self.sea_level;
let potential_loss_per_meter_in_celsius = self.max_elevation_meters  * self.celsius_loss_per_thousand_meters / 1000.0;
let total_elevation_temp_loss_in_celsius = (elevation_above_sea_level / (1.0 - self.sea_level)) * potential_loss_per_meter_in_celsius;
base_temp_from_latitude - total_elevation_temp_loss_in_celsius
}
}``````

Coloring in the above function shows a lot of mountainous area causing lower temperatures.

In the future, using ocean currents and water to help with temperature calculation would be kinda neat. In the meantime, that’s enough information to move on from temperature calculation for now.