StevenLiebregt
4/6/2018 - 1:36 PM

## Image area size calculator using resolutions and screen size

Image area size calculator using resolutions and screen size

``````/**
* Image area size calculator.
*
* This code can be used to calculate the area size of an image in squared centimeters.
*
* @author Steven Liebregt <stevenliebregt@outlook.com>
* @version 1.0.0
* @date 2018/04/06
*/

#include <iostream>
#include <cmath>

using namespace std;

/**
* Calculate the diagonal pixel size using the width and the height in pixels.
*
* @since 1.0.0
* @param width Width in pixels.
* @param height Height in pixels.
* @return The diagonal pixel size.
*/
double calculateDiagonalPixels(int width, int height)
{
double widthPixelPower = pow(width, 2);
double heightPixelPower = pow(height, 2);

return sqrt(widthPixelPower + heightPixelPower);
}

/**
* Calculate pixels per inch.
*
* @since 1.0.0
* @param diagonalPixel Diagonal size in pixels.
* @param diagonalInch Diagonal size in inches.
* @return The amount of pixels per inch.
*/
double calculatePixelPerInch(double diagonalPixel, double diagonalInch)
{
return diagonalPixel / diagonalInch;
}

/**
* Calculate the amount of inches per pixel.
*
* @since 1.0.0
* @param pixelsPerInch The amount of pixels per inch.
* @return The amount of inches per pixel.
*/
double calculateInchPerPixel(double pixelsPerInch)
{
return 1 / pixelsPerInch;
}

/**
* Convert the pixels to inches.
*
* @since 1.0.0
* @param inchPerPixel The amount of inches per pixel.
* @param pixels The amount of pixels.
* @return Inches instead of pixels.
*/
double convertPixelsToInches(double inchPerPixel, double pixels)
{
cout << inchPerPixel << endl;
cout << pixels << endl;
return inchPerPixel * pixels;
}

/**
* Convert inches to centimeters.
*
* @since 1.0.0
* @param inches The amount of inches.
* @return The amount of pixels instead of inches.
*/
double convertInchesToCm(double inches)
{
return inches * 2.54;
}

/**
* Calculate the area size by multiplying the width with the height.
*
* @since 1.0.0
* @param width The width.
* @param height The height.
* @return The area size.
*/
double calculateAreaSize(double width, double height)
{
return width * height;
}

/**
* Main function to calculate the area size in cm2
*
* @since 1.0.0
* @return
*/
int main()
{
int screenWidthPixels; // Screen width in pixels
int screenHeightPixels; // Screen height in pixels
int imageWidthPixels; // Image width in pixels
int imageHeightPixels; // Image height in pixels
double screenDiagonalInch; // Diagonal size in inches

// Get the screen width and height
printf("Give the screen resolution in pixels (width, height) in the following form [1920x1080]: \n");
scanf("%dx%d", &screenWidthPixels, &screenHeightPixels);

// Get the diagonal inch in sizes of the screen
printf("Give the screen diagonal size in inches: \n");
scanf("%lf", &screenDiagonalInch);

// Get the image width and height
printf("Give the image resolution in pixels (width, height) in the following form [640x480]: \n");
scanf("%dx%d", &imageWidthPixels, &imageHeightPixels);

// Calculate the pixels per inch
double pixelsPerInch = calculatePixelPerInch(calculateDiagonalPixels(screenWidthPixels, screenHeightPixels), screenDiagonalInch);

// Calculate the inches per pixel
double inchPerPixel = calculateInchPerPixel(pixelsPerInch);

// Calculate the width in inches and then to centimeters
double imageWidthInches = convertPixelsToInches(inchPerPixel, imageWidthPixels);
double imageWidthCentimeters = convertInchesToCm(imageWidthInches);

// Calculate the height in inches and then to centimeters
double imageHeightInches = convertPixelsToInches(inchPerPixel, imageHeightPixels);
double imageHeightCentimeters = convertInchesToCm(imageHeightInches);

// Calculate area size
double area = calculateAreaSize(imageWidthCentimeters, imageHeightCentimeters);

// Output the results
printf("Screen resolution: %dx%d \n", screenWidthPixels, screenHeightPixels);
printf("Screen diagonal (inches): %lf \n", screenDiagonalInch);
printf("\n");

printf("Image resolution: %dx%d \n", imageWidthPixels, imageHeightPixels);
printf("Image width (inches): %lf \n", imageWidthInches);
printf("Image height (inches): %lf \n", imageHeightInches);
printf("Image width (centimeters): %lf \n", imageWidthCentimeters);
printf("Image height (centimeters): %lf \n", imageHeightCentimeters);
printf("\n");

printf("Pixels per inch: %lf \n", pixelsPerInch);
printf("Inches per pixel: %lf \n", inchPerPixel);
printf("\n");

printf("Surface area (cm2): %.2lf \n", area);

return 0;
}``````