I would like to display some values into a widget in a similar way as the mechanical counters in a power meter. I have so far only written a function to split the total value into a single digit corresponding to the required position, it looks something like this:
unsigned long value; // variable holding the value to be displayed
....... get the actual value
int firstPosition = value % 10; // 0-9
int secondPosition = int(value*0.1) % 10; // 0 - 9 * 10
int thirdPosition = int(value*0.01) % 10; // 0 - 9 * 100
int fourthPosition = int(value*0.001) % 10; // 0 - 9 * 1000
int fifthPosition = int(value*0.0001) % 10; // 0 - 9 * 10000
Now the actual question, how can I perform the actual animation in order to get a similar behaviour as in a physical device? Has anybody done something similar?
Please note that I am using Qt libraries, just in case it makes a difference.
Cheers.
See the analog clock tutorial at http://qt-project.org/doc/qt-5.0/qtwidgets/widgets-analogclock.html
A one second timer is used to animate the clock, by calling update().
During the paintEvent() the widget is drawn for the current time.
Related
I please check this problem I'm creating a Time Base app but I'm having problem getting to work around the modulus oper (%) I want the remainder of 50%60 which I'm expecting to output 10 but it just give me the Lhvalues instead i.e 50. How do I go about it.
Here is a part review of the code.
void setM(int m){
if ((m+min)>59){
hour+=((min+m)/60);
min=0;
min=(min+m)%60;
}
else min+=m;
}
In the code m is passed in as 50 and min is passed in as 10
How do I get the output to be 10 for min in this equation min=(min+m)%60; without reversing the equation i.e
60%(min+m)
in C++ expression a % b returns remainder of division of a by b (if they are positive. For negative numbers sign of result is implementation defined).
you should do : 60 % 50 if you want to divide by 50
Or, if you want to get mins, i think you don't need to make min=0.
When you do 50 % 60, you get a remiainder of 50 since 50 cannot be divided by 60.
To get around this error, you can try doing do something like 70 % 60 to get the correct value as a result, since you do not want to use 60 % 50
This would follow the following logic:
Find the difference between 60 and min + m after min is set to zero if min + mis less than 60. Store it in a variable var initially set to zero.
check if the result is negative; if it is, then set it to positive by multiplying it by -1
When you do the operation, do min = ((min + m) + var) % 60; instead.
***Note: As I am unfamiliar with a Time Base App and what its purpose is, this solution may or may not be required, hence please inform me in the comments before downvoting if I there is anything wrong with my answer. Thanks!
It looks like you are trying to convert an integral number of minutes to an hour/minute pair. That would look more like this instead:
void setM(int m)
{
hour = m / 60;
min = m % 60;
}
If you are trying to add an integral number of minutes to an existing hour/minute pair, it would look more like this:
void addM(int m)
{
int value = (hour * 60) + min;
value += m;
hour = value / 60;
min = value % 60;
}
Or
void addM(int m)
{
setM(((hour * 60) + min) + m);
}
Ok some background
I have been working on this project, which I had started back in college, (no longer in school but want to expand on it to help me improve my understanding of C++). I digress... The problem is to find the Best path through a matrix. I generate a matrix filled with a set integer value lets say 9. I then create a path along the outer edge (Row 0, Col length-1) so that all values along it are 1.
The goal is that my program will run through all the possible paths and determine the best path. To simplify the problem I decide to just calculate the path SUM and then compare that to what the SUM computed by the application.
(The title is miss leading S=single-thread P=multi-threads)
OK so to my question.
In one section the algorithm does some simple bit-wise shifts to come up with the bounds for iteration. My question is how exactly do these shifts work so that the entire matrix (or MxN array) is completely traversed?
void AltitudeMapPath::bestPath(unsigned int threadCount, unsigned int threadIndex) {
unsigned int tempPathCode;
unsigned int toPathSum, toRow, toCol;
unsigned int fromPathSum, fromRow, fromCol;
Coordinates startCoord, endCoord, toCoord, fromCoord;
// To and From split matrix in half along the diagonal
unsigned int currentPathCode = threadIndex;
unsigned int maxPathCode = ((unsigned int)1 << (numRows - 1));
while (currentPathCode < maxPathCode) {
tempPathCode = currentPathCode;
// Setup to path iteration
startCoord = pathedMap(0, 0);
toPathSum = startCoord.z;
toRow = 0;
toCol = 0;
// Setup from path iteration
endCoord = pathedMap(numRows - 1, numCols - 1);
fromPathSum = endCoord.z;
fromRow = numRows - 1;
fromCol = numCols - 1;
for (unsigned int index = 0; index < numRows - 1; index++) {
if (tempPathCode % 2 == 0) {
toCol++;
fromCol--;
}
else {
toRow++;
fromRow--;
}
toCoord = pathedMap(toRow, toCol);
toPathSum += toCoord.z;
fromCoord = pathedMap(fromRow, fromCol);
fromPathSum += fromCoord.z;
tempPathCode = tempPathCode >> 1;
}
if (toPathSum < bestToPathSum[threadIndex][toRow]) {
bestToPathSum[threadIndex][toRow] = toPathSum;
bestToPathCode[threadIndex][toRow] = currentPathCode;
}
if (fromPathSum < bestFromPathSum[threadIndex][fromRow]) {
bestFromPathSum[threadIndex][fromRow] = fromPathSum;
bestFromPathCode[threadIndex][fromRow] = currentPathCode;
}
currentPathCode += threadCount;
}
}
I simplified the code since all the extra stuff just detracts from the question. Also if people are wondering I wrote most of the application but this idea of using the bit-wise operators was given to me by my past instructor.
Edit:
I added the entire algorithm for which each thread executes on. The entire project is still a work a progress but here is the source code for the whole thing if any one is interested [GITHUB]
A right bit shift is equivalent to dividing by 2 to the power of the number of bits shifted. IE 1 >> 2 = 1 / (2 ^ 2) = 1 / 4
A left bit shift is equivalent to multiplying by 2 to the power of the number of bits shifted. IE 1 << 2 = 1 * 2 ^ 2 = 1 * 4
I'm not entirely sure what that algorithm does and why it needs to multiply by 2^ (num rows - 1) and then progressively divide by 2.
(this function is part of a larger program but operates independently of other functions.)
Ok I have a function jacket and given 3 inputs it produces the correct output only 75% of the time.
I do not know the inputs but I know the output is wrong.
I do not know what is wrong and have no idea how to fix it.
I assume it is the same 12 values entered each time the function is submitted to myProgrammingLab.
So it may be a problem with a specific input.
Thanks.
The Description:
Jacket size (chest in inches) = height times weight divided by 288 and then adjusted by adding 1/8 of an inch for each 10 years over age 30. (note that the adjustment only takes place after a full 10 years. So, there is no adjustment for ages 30 through 39, but 1/8 of an inch is added for age 40.)
edit: changing tmp to float still produced the error.
float jacket(float weight, float height, int age) {
double result = (height * weight) / 288;
/*now for every 10 years past 30 add (1/8) to the result*/
if((age - 30) > 0){
int temp = (age - 30) / 10;
result = result + (temp * .125);
}
return result;
}
This is the same function written differently with the same problem.
float jacket(double jWeight, double jHeight, int jAge)
{
double jSize = ((jWeight*jHeight)/288.0);
int i = jAge/10 - 3;
if((jAge/10)>3)
jSize += 0.125*i;
return jSize;
}
This is a third function with the same problem
float jacket(double weight, double height, int age)
// calculates the jacket size, adjusting for age in increments
// of ten years, if customer is over 30 years of age.
{
int age_factor;
double j_size;
j_size = (height*weight)/288.0;
if (age >=30)
{
age_factor = (age-30)/10; //note possible truncation.
j_size += age_factor/8.0;
}
return j_size;
}
The first time the function is called it produces an incorrect return value. the remain 3 times it is called the return value is correct.
Observe:
Expected Output:
jacket·size·=·24.17↵
jacket·size·=·40.00↵
jacket·size·=·46.04↵
jacket·size·=·35.42↵
Actual Output:
jacket·size·=·24.29↵
jacket·size·=·40.00↵
jacket·size·=·46.04↵
jacket·size·=·35.42↵
*All three functions given the same input produce the same output
int temp = (age - 30) / 10;
By making temp an int, you will get incorrect results, because of truncation. Try using a float instead.
I guess one could say that 31 isn't necessarily 1 year over 30. Try int temp = (age - 31)/10;. Personally I think that's wrong, as does everyone else here, but someone could argue that the moment one turns 31 one is only a few seconds over 30. It's worth a try, anyway.
I have a slider control from a GUI that gives values from 1 to 400, I need to assign only odd numbers from the slider to a variable, but I dont know how to get just the odd numbers from it, any help will be much appreciated
Assuming that the slider has an interface that lets you check its value you can do something like this:
Slider* slider = However you get your slider;
int sliderValue;
// Use modulus division to determine if slider value is odd
if(slider->getValue() % 2 == 1) {
sliderValue = slider->getValue();
}
Assuming like Todd that the slider has an interface that lets you check its value I would take the value and reduce it by 1 iff it's even:
Slider* slider = However you get your slider;
int sliderValue = slider->getValue();
sliderValue -= (1 - sliderValue % 2);
I have to implement small multimage graphic control, which in essence is an array of 9 images, shown one by one. The final goal is to act as minislider.
Now, this graphic control is going to receive various integer ranges: from 5 to 25 or from 0 to 7 or from -9 to 9.
If I am going to use proportion - "rule of three" I am afraid is not technically suistainable because it can be a source of errors. My guess is to use some lookup tables, but has anyone an good advice for approach?
Thnx
I'm not sure look up tables are required. You can get from your input value to an image index between 0 and 9 proportionally:
int ConvertToImageArrayIndex(int inputValue)
{
int maxInputFromOtherModule = 25;
int minInputFromOtherModule = 5;
// +1 required so include both min and max input values in possible range.
// + 0.5 required so that round to the nearest image instead of always rounding down.
// 8.0 required to get to an output range of 9 possible indexes [0..8]
int imageIndex = ( (float)((inputValue-minInputFromOtherModule) * 8.0) / (float)(maxInputFromOtherModule - minInputFromOtherModule + 1) ) + 0.5;
return imageIndex;
}
yes, a lookup table is a good solution
int lookup[9] = {5, 25, ... the other values };
int id1 = floor(slider);
int id2 = id1+1;
int texId1 = lookup[id1];
int texId2 = lookup[id2];
interpolate(texId1, texId2, slider - float(id1));