Working with Floating Point Values and Integers in VB.NET

Drawer GTIN - 12 in VB.NET Working with Floating Point Values and Integers

Working with Floating Point Values and Integers
Generate UPC-A In VB.NET
Using Barcode drawer for Visual Studio .NET Control to generate, create UPCA image in .NET applications.
www.OnBarcode.com
UPC-A Supplement 5 Scanner In VB.NET
Using Barcode scanner for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
The previously mentioned displayWidth and displayHeight variables have been declared as floating point, although the display properties themselves are integers. This is because all your calculations involving the width and height of items need the fractional part that floating point variables give you.
Barcode Printer In VB.NET
Using Barcode generator for .NET framework Control to generate, create barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Barcode Reader In Visual Basic .NET
Using Barcode scanner for .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Part III
Printing GTIN - 12 In C#
Using Barcode printer for Visual Studio .NET Control to generate, create UPC-A Supplement 2 image in .NET applications.
www.OnBarcode.com
Create UPC Symbol In Visual Studio .NET
Using Barcode generation for ASP.NET Control to generate, create UPCA image in ASP.NET applications.
www.OnBarcode.com
Games and Programming
Drawing UPC-A Supplement 2 In VS .NET
Using Barcode printer for Visual Studio .NET Control to generate, create UPC A image in Visual Studio .NET applications.
www.OnBarcode.com
Creating Bar Code In VB.NET
Using Barcode generation for Visual Studio .NET Control to generate, create bar code image in VS .NET applications.
www.OnBarcode.com
However, all the properties of your display rectangle are integers, so you need to convert these floating point values into integers when you want to position the sprites. You know that you can use casting to convert from one type to another, but you also need to allow for the fact that casting always truncates; in other words, if the floating point input were 1.99999, that input would still be converted to 1, which would be inaccurate. You can make sure that the converted value is as accurate as possible by adding 0.5 to the floating point value before you truncate it so that 1.99999 would turn into 2.4999 and then be truncated down to 2. You can see this in action in the scaleSprites method later in this chapter.
Bar Code Generation In VB.NET
Using Barcode creator for VS .NET Control to generate, create barcode image in .NET framework applications.
www.OnBarcode.com
Drawing GTIN - 13 In Visual Basic .NET
Using Barcode encoder for .NET Control to generate, create EAN / UCC - 13 image in Visual Studio .NET applications.
www.OnBarcode.com
Double Precision Floating Point Values
Print Matrix Barcode In VB.NET
Using Barcode creation for VS .NET Control to generate, create Matrix Barcode image in .NET applications.
www.OnBarcode.com
Making Leitcode In Visual Basic .NET
Using Barcode creator for .NET Control to generate, create Leitcode image in .NET framework applications.
www.OnBarcode.com
C# and XNA can use two different types to hold floating point values. One of these types is called float and holds a number with seven digits of precision. This means it could hold the mathematical constant pi as 3.14159274. The other type is called double. This second type uses twice as much memory to hold each value and is good for around 16 digits of accuracy, and could hold pi as 3.14159265358979. Very high levels of precision can be important in video games because values are being updated thousands of times a second, so errors will accumulate quickly. For the purpose of your game, you can use floating point. However, you need to remember that when you give a real number value (one with a decimal point) in the program text, the C# compiler will assume that it s a double precision value. So the following statement would cause a compilation error:
QR Code Reader In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Create UPC A In .NET Framework
Using Barcode printer for Reporting Service Control to generate, create UPCA image in Reporting Service applications.
www.OnBarcode.com
float pi = 3.14159;
Generating GTIN - 12 In None
Using Barcode creation for Online Control to generate, create GTIN - 12 image in Online applications.
www.OnBarcode.com
Code 128 Code Set A Drawer In Java
Using Barcode encoder for Android Control to generate, create Code 128 Code Set A image in Android applications.
www.OnBarcode.com
The error is caused because 3.14159 is compiled as a double precision value, and you know that the C# compiler hates it when you perform an action that might result in a loss of data (which is what would happen if you put a double value into a float variable). There are two ways around this: you can cast the double value to floating point, or you can change the value in the program to be a floating point value, as shown here:
Scanning Code 128 In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Code 39 Full ASCII Recognizer In None
Using Barcode recognizer for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
float pi = 3.14159f;
Paint EAN / UCC - 14 In None
Using Barcode maker for Software Control to generate, create EAN128 image in Software applications.
www.OnBarcode.com
GTIN - 13 Creation In None
Using Barcode generator for Font Control to generate, create EAN13 image in Font applications.
www.OnBarcode.com
Putting the letter f after a floating point tells the compiler that you re writing a floating point value, not a double precision value.
Drawing and Aspect Ratios
When an image is drawn, you need to be careful to preserve its aspect ratio. The aspect ratio of an image is the ratio of the width to the height. For your cheese, this is just about 1 because the texture is square, but for your bread, it s quite different. Figure 11-6 shows the effect of getting the aspect ratio wrong when you draw the bread bat. The bread has an aspect ratio of around 4 to 1, in other words, it is around four times as wide as it is high. The program can get the aspect ratio of the original image from the dimensions of the texture.
11
A Game as a C# Program
Correct Aspect Ratio
Distorted Aspect Ratio
Figure 11-6 The effect of aspect ratio on drawing
float aspectRatio = (float) cheeseTexture.Width / cheeseTexture.Height;
The program can now use the aspect ratio to calculate the correct height of a sprite given the width that we want it to have. Note The variable aspectRatio is being declared and used in the program to hold a value which is going to be used in one particular part of the code. This is called a local variable as it is only used in one place in the code and has no need to be visible anywhere else.
Copyright © OnBarcode.com . All rights reserved.