Converting height in centimeters to inches but cant get decimal points - android

Trying to convert centimeters to inches then round to the nearest half inch and print 1 decimal point.
3.1 = 3.0
3.2 = 3.0
3.3 = 3.5
3.6 = 3.5
3.8 = 4.0
float index;
float height;
index = (Math.round((height * .393701)*2))/2;
text.setText("Index: " + index);
When I print index it wont show the decimal. Once the number reaches .75 or higher it rounds to the next higher whole number.

Try this,
float index;
float height;
index = (float) (Math.round((height / 2.54) * 10) / 10.0);
text.setText("Index: " + index);

I put an f at the end of each number.
(Math.round((height / 2.54f)*2f)/2f)
Height
147 / 2.54 = 57.874016 = 58
148 / 2.54 = 58.26772 = 58.5

Related

Incorrect frequency on manually drawn graph

I am currently writting a a spectrum analyzer for android for university and part of this involves plotting the FFT of sound. However, I am having an issue with plotting the frequencies. The freq values start off correct, but as i move to higher frequencies the error is becoming greater and greater (at 3000Hz, the graph will show ~3750). I feel as though there is an error in the way I am calculating the x-axis or freq values. This is a manually drawn graph for speed purposes.
If more info/code is needed just let me know, but my guess is that it is something simple that I have overlooked. Thanks
xVal is the frequency value. and the scale value is to scale it according to the real graph dimensions.
int length = currentWaveDataDouble.length;
int pow2 = Integer.highestOneBit(length) << 1;
int sampleRate = 44100;
...
//actual plot part
for(int i =0; i<p2.length; i++) {
float xVal = (float)(i * scaleX.ScaleValue(((double) sampleRate / (pow2 >> 1))));
if (xVal < maxFreqPlus1) {
xVal += axisWidth + yAxisMargin;
float yVal = (float) scaleY.ScaleValue(p2[i]);
yVal += axisWidth + xAxisMargin;
canvas.drawPoint(xVal,yVal, marker);
if(yVal > yMax)
{
yMax = yVal;
xMax = xVal;
}
}
}
Freq generator set to 4000 Hz
Freq generator set to 1000 Hz (value is 1250Hz)
Found the issue. it was in the scaler.
ValueScaler scaleY = new ValueScaler(0,maxAmpPlus1 - yAxisMargin,0,baseY);
ValueScaler scaleX = new ValueScaler(0,maxFreqPlus1 - xAxisMargin,0,baseX);
i wasn't taking into account the x and y margin when scaling the numbers.

Calculate minimum readable textSize for Android tablets & tv devices

For Android Tv, Android recommends Designing your artwork assets for best viewing at HD resolution (1920 x 1080 pixels).
There are following two available resolutions for TVs:
1280x720
1920x1080
Main two screen sizes available for android Tablets are 7 inch & 10 inch. This incurs in following two key resolutions(Considered from here):
7” tablets: 1024x600
10” tablets: 1280x800
Also, for different display devices we can have different resolution for Android TV devices and for tables for also.
I needs to calculate minimum readable textSize for each device for given message of given length.
Lets give me an example, User sets maximum message length to n chars. Now, I want to restrict user for this character length for specific device. So that i can have maximum length of message with minimum fontSize that is readable to users for all TV & tablet devices. It would be fine if we have lower limit, but maximum character limit should not make TextView scroll-able.
Initially I tried to calculate maximum message length as per fontSize of textview like following:
TextView text = (TextView) findViewById(R.id.text);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
int dens = dm.densityDpi;
double wi = (double) width / (double) dens;
double hi = (double) height / (double) dens;
double x = Math.pow(wi, 2);
double y = Math.pow(hi, 2);
double screenInches = Math.sqrt(x + y);
//long charLimit = (long) (Math.pow(Math.floor((dens / 18)), 2) * 2);
long charLimit = (long) (((Math.floor((dens / 26)) * wi) * (Math.floor((dens / 26)) * hi)) / (wi + hi));
//long charLimit = (long) ((Math.floor((dens / 22)) * Math.floor((x + y)) * 2));
String msg = "";
for (int i = 0; i < charLimit; i++) {
msg += "A";
}
text.setText(msg);
Log.e("screenInches", "screenInches: " + screenInches + " width:" + width + "/" + wi + " height:" + height + "/" + hi + " densityDpi:" + dens + " charLimit:" + charLimit);
But couldn't get satisfied results. Now,All what i want is to calculate minimum readable fontSize (say S), for given length(say n char) of message to be displayed without scroll on both Tv and tablet devices.
I know font-family and font-style also affects this. But considering system default configurations which are proper approaches, I should follow?
This is not possible to do very well with variable-width fonts. Each character I'm writing has a different width and this means each character's width has to be computed separately. A monospace font would be easier to compute.

Circle overlay to map-distance conversion

I have a circular overlay that can change according to the user's preferences: they have a circle of radius 'r' around them and a slider can change 'r' accordingly. So far it works perfectly well.
My problem is I don't know how to find the proper conversion from the circle's radius to the map's metric. As an example, given the circle below, what is the distance covered with the given radius?
You must know what is the scale of the map you are showing. Having scale of the map you could convert pixels size to metric size. And having this proportion you can convert radius given in pixels to meters.
So here's how you do it...
Jedil is correct you have to find your screen width/height, the dpi of the screen, etc. So, looking through this old osmdroid source I sorta figured out this:
//Get the x and y dpi
this.xdpi = this.context.getResources().getDisplayMetrics().xdpi;
this.ydpi = this.context.getResources().getDisplayMetrics().ydpi;
//Get the screen width/height
this.screenWidth = this.context.getResources().getDisplayMetrics().widthPixels;
this.screenHeight = this.context.getResources().getDisplayMetrics().heightPixels;
// DPI corrections for specific models
String manufacturer = null;
try {
final Field field = android.os.Build.class.getField("MANUFACTURER");
manufacturer = (String) field.get(null);
} catch (final Exception ignore) {
}
if ("motorola".equals(manufacturer) && "DROIDX".equals(android.os.Build.MODEL)) {
// If the screen is rotated, flip the x and y dpi values
WindowManager windowManager = (WindowManager) this.context
.getSystemService(Context.WINDOW_SERVICE);
if (windowManager.getDefaultDisplay().getOrientation() > 0) {
this.xdpi = (float) (this.screenWidth / 3.75);
this.ydpi = (float) (this.screenHeight / 2.1);
} else {
this.xdpi = (float) (this.screenWidth / 2.1);
this.ydpi = (float) (this.screenHeight / 3.75);
}
} else if ("motorola".equals(manufacturer) && "Droid".equals(android.os.Build.MODEL)) {
// http://www.mail-archive.com/android-developers#googlegroups.com/msg109497.html
this.xdpi = 264;
this.ydpi = 264;
}
// set default max length to 1 inch
maxLength = 2.54f;
That's how you get the 'constants' (at least in the device's eyes). To convert...
// calculate dots per centimeter
int xdpcm = (int) ((float) xdpi / 2.54);
int ydpcm = (int) ((float) ydpi / 2.54);
// get length in pixel
int xLen = (int) (maxLength * xdpcm);
int yLen = (int) (maxLength * ydpcm);
// Two points, xLen apart, at scale bar screen location
IGeoPoint p1 = projection.fromPixels((screenWidth / 2) - (xLen / 2), yOffset);
IGeoPoint p2 = projection.fromPixels((screenWidth / 2) + (xLen / 2), yOffset);
// get distance in meters between points
final int xMeters = ((GeoPoint) p1).distanceTo(p2);
...and it's a similar, if not near-identical, matter to get the yMeters (hint: use screenHeight).
Admittedly, I'm not totally sure of what the lines IGeoPoint... are doing but I realize the conversion is there. Hoping this helps someone out in the future. For a better understanding of the above code, please see the link I've posted.
I think what you're looking for is a meters-to-pixels calculation. You can get this from the projection:
// Get projection
Projection proj = mMapView.getProjection();
// How many pixels in 100 meters for this zoom level
float pixels = proj.metersToPixels(100);
// How many meters in 100 pixels for this zoom level
float meters = 1 / proj.metersToPixels(1 / 100);
// You could also get a raw meters-per-pixels value by using TileSystem.GroundResolution()
Two things to remember - this value will change not only based on what zoom level but based on what latitude you are at on the maps.

OpenGL es 2.0 and 16 bit channel

It possible to return 16 bit value from fragment shader on Android devices?
I've made this conversion to convert 16 bit "x" value to 4444:
vec4 convertToVec4(float x)
{
int iX = int(65535.0 * x);
int r = (iX / (0x1000));
int g = (iX / (0x100)) - r*0x10;
int b = (iX / (0x10)) - (r*0x100 + g*0x10);
int a = (iX) - (r*0x1000 + g*0x100 + b*0x10);
return vec4(float(r)/15.0, float(g)/15.0, float(b)/15.0, float(a)/15.0);
}
and to get back 16 bit "x" from 4444:
float getFloat(vec4 v)
{
vec4 col = v * 15.0;
int sum = int(col.r*4096.0) + int(col.g*256.0) + int(col.b*16.0) + int(col.a);
return float(sum) / 65535.0;
}
It works fine but it is very slow. Is there some way to pass 16 bit color in one channel (eg. red or alpha)?
Two functions are in Shader ? If so you had better calculate that in cpu with neon and then gives the returned value to GLSL
Why don't you use 32 bit channel instead of 16 bit ?

how to draw open ended circle in open gl es android

I need to draw a open ended circle as shown in image attached below
http://i49.tinypic.com/254y5bs.png
In the image the length of M should be greater than N, and the starting points of lines M and N are the center of circle. How can I draw an arc from the end point of N to M such that the arc looks linear in shape.
i am drawing the circle using the following code
for (int nR = N_IN_DEGREE; nR < M_IN_DEGREE && nCount < 360; nR++)
{
float fX = (float) Math.sin((float) nR * (Math.PI / 180)) + nR * 0.0008f;
float fY = (float) Math.cos((float) nR * (Math.PI / 180));
stVertexArray[nCount * 2] = fX;
stVertexArray[nCount * 2 + 1] = fY;
nCount++;
}
I get the open end circle from the angles N_IN_DEGREE to M_IN_DEGREE..but as I am increasing the value of fX by a factor of nR * 0.0008f , the complete circle is drawn only with 350 degrees,but i want 360..please run the code and see..my requirement is that I need to draw 2 lines n(length 0.8) and line m(length=1) from a single point and draw a arc from end point of n to end point of m and fill the arc with a color as well..
I can't make your code run, as there is not enough information, and I have trouble following your code.
But here is the basic idea, implemented in python.
import math
def genc(r1, r2, t1, t2, n):
dr = (r2 - r1)/n
dt = (t2 - t1)/n
x = []
y = []
for k in range(n):
r = r1 + k*dr
t = t1 + k*dt
x.append(r*math.cos(t))
y.append(r*math.sin(t))
return [x, y]
This is the output (plotted using matplotlib)
In [43]: a = circ.genc(1.0, 0.8, math.pi/2, 2*math.pi, 60)
In [44]: plot(a[0], a[1],'ro')
Out[44]: [<matplotlib.lines.Line2D at 0xb9285d0>]

Categories

Resources