Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I am trying to program a speedometer! I have a needle image, and I want the needle to rotate according to the current download speed.
I have the following method, but it is not working. When i update my needle with CurrentDegress, it never goes down, only higher and higher; the needle rotates around the circle...Anyone?!
- (void) recalculateDegrees:(CGFloat) currentlyLoadedBytes
{
if(bytes <= 0){
currentDegrees = 0;
}
else if (bytes > completeFileSize){
currentDegrees = MAX_DEGREES;
}
else {
CGFloat log_tmp =(CGFloat) log10f(currentlyLoadedBytes / 1000);
currentDegrees = (log_tmp/5.0f)*290.0f;
}
//code to rotate image to currentDegrees
}
You can use the following method to get the angle from the speed value (and not from currentlyLoadedBytes):
- (CGFloat)angleFromSpeed:(CGFloat)speed
{
if (speed <= 0) {
return MIN_ANGLE;
} else if (speed >= MAX_SPEED) {
return MAX_ANGLE;
} else {
return speed / MAX_SPEED * (MAX_ANGLE - MIN_ANGLE) + MIN_ANGLE;
}
}
I don't follow 100% of what you are doing, but somewhere in your else block you should be dividing currentlyLoadedBytes / completeFileSize. Also, is bytes supposed to be cuurentlyLoadedBytes?
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to implement a command line application in C/Objective C which will act as a calculator of more than two numbers.
E.g ./calc 5 + 4 * 6
= 29
I just need an idea or simple algorithm to start. I will appreciate any help on this.
The algorithm you want is the infix notation to postfix notation converter.
You can find some more info on it over here.
http://scriptasylum.com/tutorials/infix_postfix/algorithms/infix-postfix/index.htm.
EDIT: I am not sure if this will help, but here is an implementation in Java. I'm not familiar with Objective-C
// converts a infix string to postfix string
private void convertInfixToPostfix(){
// create an empty operand stack
operatorStack = new Stack<>();
Operator operator = null;
Operand operand = null;
for(int i = 0; i < expressionTokens.size(); i++){
String token = expressionTokens.get(i);
Element element = new Element(token);
if(element.isOperand(token)){ // check if element is operand
// add the element to the postfix string
operand = new Operand(element.getStringValue());
postFixString.add(operand);
}
else if(operatorStack.isEmpty()){
// push the token to the operator stack, its an operator
operator = new Operator(element.getStringValue());
operatorStack.push(operator);
}
else {
operator = new Operator(element.getStringValue());
while(!operatorStack.isEmpty() &&
(operatorStack.peek().getPrecedence()
<= operator.getPrecedence()))
postFixString.add(operatorStack.pop());
operatorStack.push(operator);
}
}
// add the rest of the operator stack to the postfix string
while(!operatorStack.isEmpty()){
Operator remainingOperator = operatorStack.pop();
postFixString.add(remainingOperator);
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have one part code objective c code and I want to convert to swift :
NSString *rawStr = [[tokenField textField] text];
for (int i = 0; i < [rawStr length]; i++)
{
if (![charSet characterIsMember:[rawStr characterAtIndex:i]])
{
[recipient appendFormat:#"%#",[NSString stringWithFormat:#"%c", [rawStr characterAtIndex:i]]];
}
}
if ([rawStr length])
{
[tokenField addTokenWithTitle:rawStr representedObject:recipient];
}
please guide me about that.
If you don't mind using NSString the port is straight forward:
let rawStr:NSString = tokenField.textField.text
for i in 0..<rawStr.length {
let currentChar = rawStr.characterAtIndex(i)
if !charSet.characterIsMember(currentChar) {
recipient.appendFormat("%#", NSString(format:"%c", currentChar))
}
}
if rawStr.length > 0 {
tokenField.addTokenWithTitle(rawStr, representedObject:recipient)
}
Else the String class does not have a length method. You'll have to use s.startIndex.advancedBy syntax.
Looks like you are trying to keep characters that are not in your character set.
func strRemoveCharsNotInSet(str: String) -> String {
let charSet = NSCharacterSet(charactersInString: ".#")
let temp = str.componentsSeparatedByCharactersInSet(charSet)
let backToString = temp.joinWithSeparator("")
return backToString
}
print(strRemoveChars("Hello#There.Friend"))
outputs:
HelloThereFriend
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm getting Expected expression errors on the following code:
(void) for(t; t < kPlatformsStartTag + kNumPlatforms; t++) { //error here
CCSprite *platform = (CCSprite*)[batchNode getChildByTag:t];
CGSize platform_size = platform.contentSize;
CGPoint platform_pos = platform.position;
max_x = platform_pos.x - platform_size.width/2 - 10;
min_x = platform_pos.x + platform_size.width/2 + 10;
float min_y = platform_pos.y + (platform_size.height+bird_size.height)/2 - kPlatformTopPadding;
if(bird_pos.x > max_x &&
bird_pos.x < min_x &&
bird_pos.y > platform_pos.y &&
bird_pos.y < min_y) {
[self jump];
}
}
(void) for(t; t < kCloudsStartTag + kNumClouds; t++) { //error here
CCSprite *cloud = (CCSprite*)[batchNode getChildByTag:t];
CGPoint pos = cloud.position;
pos.y -= delta * cloud.scaleY * 0.8f;
if(pos.y < -cloud.contentSize.height/2) {
currentCloudTag = t;
[self resetCloud];
} else {
cloud.position = pos;
}
}
The error is found where the "for" code is. I put the (void) code in because I will get an Expression result unused error. Any ideas?
The (void) before the for loop does not make sense.
You have to remove the (void) before the for loop because it's not a valid c syntax. You can't solve an error with another error.
You may ask the question : Why puting (void) before the for loop prevented the unused expression error. Well that's because the debugger didn't reach it. and it doesn't know for what is for as he expected a resulted value from it to cast it to void.
When the compiler is generating the error: Unused Entity Issue - Expression result unused. That's means that your program is evaluating an expression without using it.
In your case at the for loop if the t variable is already initialized as you want it, you shouldn't put it at the first part as it will be considired as an unused expression.
for(; t < kPlatformsStartTag + kNumPlatforms; t++) { // keep the first expresion empty
// ...
}
You've already got answers about the bogus (void), but not about the unused expression.
for(t; t < kCloudsStartTag + kNumClouds; t++)
The initial expression here, t, has absolutely no effect, and for that reason has no business being present at all. The value of t is read and immediately discarded, and any decent compiler will optimise that by not even bothering to read t. You do not need an expression here. You can remove it, and write
for(; t < kCloudsStartTag + kNumClouds; t++)
although personally, I might be tempted to go with a while loop instead.
Edit: reading your code more closely, your code seems to need to give t an initial value.
for(t = 0; t < kCloudsStartTag + kNumClouds; t++)
Either way, your attempt to suppress the warning without understanding what the warning was telling you wasn't a good idea.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have a center point and i know point 1, now i want to calculate point 2 which is in the opposite direction, but have another lenght. Also i know the lenght from the center point to point 2, but it is not on the same vector as Point 1 to center is. Imagine the dotted line has another angle as the image shows.
Point 2 should be on the same vector as is Point 1. In other words, Point 1 to Point 2 should be a straight line which pass trough the center.
I hope some one can help me.
Thank you very much in advance.
This will work:
Code assumes collinear points, in a 2D plane, defined as Cartesian coordinates
In Java:
class GoGo {
public static void main (String[] args) {
double[] center = new double[] { 4.0,3.0 };
double[] point1 = new double[] { 8.0,4.0 };
double[] point2 = getPoint2(center,point1,4.0);
System.out.print("X: " + point2[0] + " Y: " +point2[1]);
}
public static double[] getPoint2(double[] center, double[] point1, double distance) {
//assumes Points = double[2] { xValue, yValue }
double[] point2 = new double[2];
double changeInX = point1[0] - center[0]; // get delta x
double changeInY = point1[1] - center[1]; // get delta y
double distanceCto1 = Math.pow( // get distance Center to point1
(Math.pow(changeInX,2.0) + Math.pow(changeInY,2.0))
,0.5);
double distanceRatio = distance/distanceCto1;// ratio between distances
double xValue = distanceRatio * changeInX; // proportional change in x
double yValue = distanceRatio * changeInY; // proportional change in y
point2[0] = center[0] - xValue; // normalize from center
point2[1] = center[0] - yValue; // normalize from center
return point2; // and return
}
}
I wrote this in Java because it is my preferred language and you didn't specify a language you needed the answer in . If you have a different language preference, I can attempt to port the code to your preferred language (assuming I know it).
CODE GIVEN BY: Marcello Stanisci
In Objective C:
- (CGPoint) getOppositePointAtCenter2:(CGPoint)center fromPoint:(CGPoint)point oppositeDistance:(double)oppositeDistance {
CGPoint vector = CGPointMake(point.x - center.x, point.y - center.y);
double distanceCenterToPoint1 = pow(pow(vector.x, 2) + pow(vector.y, 2), 0.5);
double distanceRatio = oppositeDistance / distanceCenterToPoint1;
double xValue = distanceRatio * vector.x;
double yValue = distanceRatio * vector.y;
return CGPointMake(center.x - xValue, center.y - yValue);
}
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
How can i get a process by know process name on mac os?
Just reformatted Haley's answer:
// Return YES if given name process in process list . Otherwise return NO
bool IsInBSDProcessList(char *name) {
assert( name != NULL);
kinfo_proc *result;
size_t count = 0;
result = (kinfo_proc *)malloc(sizeof(kinfo_proc));
if(GetBSDProcessList(&result,&count) == 0) {
for (int i = 0; i < count; i++) {
kinfo_proc *proc = NULL;
proc = &result[i];
if (strcmp(name, proc->kp_proc.p_comm) == 0) {
free(result);
return true;
}
}
}
free(result);
return false;
}
Your question is rather vague. Can you define what you mean by "get a process"?
One method (depending on your definition): launch the Activity Monitor app in Applications/Utilities, and look up the process name in the list.
Perhaps
ps -eaf
from a console