Compare two CGSize's to best persentage - objective-c

Need your help, I have input CGSize (for example): 200x300. And array with other CGSize's = [20x20, 100x100, 150x150, 200x100, 200x250, 300x300...].
Please help me to find best item in array that have best compare percentage (for example its 200x250)...
I tried to use for enumerator, for example:
CGSize inputSize = CGSizeMake(200, 300);
for (int i = 0; i < array.count; i++)
{
CGSize concurentSize = CGSizeZero;
switch (i)
{
case 0:
{
concurentSize.width = 20;
concurentSize.height = 20;
}
and so on...
float differencePercentWidth = ( concurentSize.width / inputSize.width ) * 100.0;
float differencePercentHeight = ( concurentSize.height / inputSize.height ) * 100.0;
if (differencePercentWidth > 90 && differencePercentHeight > 90)
{
// FOUND best CGSize... stop
break.
}
}
But, its not working, it differencePercentWidth/differencePercentHeight can be > 100 =(
I need some of method or function that can compare 2 CGSize's in percent match... For example: size 200x300 is best matches with size 200x250... Something like:
float matchesInPerсent = CGSizeCompare(firstCGSize, secondCGSize);
//matchesInPerсent = 0.6; // in percents
Please help, sorry for my english, if you need more details, please let me know. Thanks.

Try the similar logic to calculate the maximum number in the array, but need to less then one finite value. In this cases calculate maximum percentage average of size.width and size.height, the maximum percentage which is closes to 1 is the winner. If you need the upper value of the 100% as well then you need to insert the logic to take that value below the 100% and run the same logic on those sizes as well.
Here is code which will give you closest percentage size from the array.
/*
sizes : array of the sizes represented in NSValue format
size: The size for which you need closest value.
*/
- (CGSize)bestMatch:(NSArray *)sizes withSize:(CGSize)size {
float bestMatch = 0.0;
CGSize bestMatchSize = CGSizeZero;
for (NSValue *value in sizes) {
float percentage = (value.CGSizeValue.width/size.width + value.CGSizeValue.height/size.height)/2;
//If you need greater then 100% and closes to the size
if (percentage > 1.0) {
percentage = -1*(percentage - 2);
}
if (bestMatch < percentage && percentage < 1) {
bestMatch = percentage;
bestMatchSize = value.CGSizeValue;
}
}
//If you need best match you can return bestMatch which is closest in percentage
return bestMatchSize;
}

Related

Check if image is dark-only bottom part

I am checking if UIImage is darker or more whiter . I would like to use this method ,but only to check the third bottom part of the image ,not all of it .
I wonder how exactly to change it to check that,i am not that familiar with the pixels stuff .
BOOL isDarkImage(UIImage* inputImage){
BOOL isDark = FALSE;
CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(inputImage.CGImage));
const UInt8 *pixels = CFDataGetBytePtr(imageData);
int darkPixels = 0;
long length = CFDataGetLength(imageData);
int const darkPixelThreshold = (inputImage.size.width*inputImage.size.height)*.25;
//should i change here the length ?
for(int i=0; i<length; i+=4)
{
int r = pixels[i];
int g = pixels[i+1];
int b = pixels[i+2];
//luminance calculation gives more weight to r and b for human eyes
float luminance = (0.299*r + 0.587*g + 0.114*b);
if (luminance<150) darkPixels ++;
}
if (darkPixels >= darkPixelThreshold)
isDark = YES;
I can just crop that part of the image, but this will be not efficient way, and wast time .
The solution marked correct here is a more thoughtful approach for getting the pixel data (more tolerant of differing formats) and also demonstrates how to address pixels. With a small adjustment, you can get the bottom of the image as follows:
+ (NSArray*)getRGBAsFromImage:(UIImage*)image
atX:(int)xx
andY:(int)yy
toX:(int)toX
toY:(int)toY {
// ...
int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel;
int byteIndexEnd = (bytesPerRow * toY) + toX * bytesPerPixel;
while (byteIndex < byteIndexEnd) {
// contents of the loop remain the same
// ...
}
To get the bottom third of the image, call this with xx=0, yy=2.0*image.height/3.0 and toX and toY equal to the image width and height, respectively. Loop the colors in the returned array and compute luminance as your post suggests.

Distance between point and finite line in objective-c

I've looked up some formulas relating to finding the distance a point and a line. On this page, I used example 14
http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
I have a method that has turned into this:
+(bool) checkPointNearBetweenPointsWithPointA:(CGPoint)pointA withPointB:(CGPoint)pointB withPointC:(CGPoint)pointC withLimit:(float)limit {
float A = pointB.x - pointA.x;
float B = pointA.y - pointC.y;
float C = pointA.x - pointC.x;
float D = pointB.y - pointA.y;
float dividend = fabs( A * B ) - ( C * D );
float divisor = sqrt(pow(A,2) + pow(D,2));
float distanceBetweenPointAndLine = dividend / divisor;
if(distanceBetweenPointAndLine < limit){
NSLog(#"distanceBetweenPointAndLine = %f",distanceBetweenPointAndLine);
return YES;
}
return NO;
}
The problem is that it still returns YES if I'm passed point B, if the line segment is drawn like B----A. Other screwed up things happen to depending on which angle the line is drawn. Just wondering if I need to consider anything else if testing to see if a point is near a finite line. Most examples I see online deal with lines of infinite length.
try my code below. line is considered to exist between points A & B (regardless of how you draw it B->A or A->B ) and point C is the point in consideration to measure the distance.
+ (bool) checkPointNearBetweenPointsWithPointA:(CGPoint)pointA
withPointB:(CGPoint)pointB
withPointC:(CGPoint)pointC
withLimit:(float)limit
{
CGFloat slopeLine = atan((pointB.y-pointA.y)/(pointB.x-pointA.x) );
CGFloat slopePointToPointA = -1 *atan((pointC.y-pointA.y)/(pointC.x-pointA.x));
CGFloat innerAngle = slopeLine + slopePointToPointA;
CGFloat distanceAC = sqrtf(pow(pointC.y-pointA.y,2) + pow(pointC.x-pointA.x,2));
CGFloat distanceBetweenPointAndLine = fabs(distanceAC * sin(innerAngle));
NSLog(#"distanceBetweenPointAndLine = %f",distanceBetweenPointAndLine);
NSLog(#"is exceeding limit ? %#",distanceBetweenPointAndLine > limit ? #"YES":#"NO");
if(distanceBetweenPointAndLine < limit)
{
return YES;
}
return NO;
}

Getting minimum number of MKMapRects from a MKPolygon

So I have a function that takes two MKMapRect's and the second intersects with the first one. So the function creates an MKPolygon that is the first rect without the intersecting parts:
-(void) polygons:(MKMapRect)fullRect exclude:(MKMapRect)excludeArea{
NSLog(#"Y is: %f height: %f",excludeArea.origin.y,excludeArea.size.height);
double top = excludeArea.origin.y - fullRect.origin.y;
double lft = excludeArea.origin.x - fullRect.origin.x;
double btm = (fullRect.origin.y + fullRect.size.height) - (excludeArea.origin.y + excludeArea.size.height);
double rgt = (fullRect.origin.x + fullRect.size.width) - (excludeArea.origin.x + excludeArea.size.width);
double ot = fullRect.origin.y, it = (ot + top);
double ol = fullRect.origin.x, il = (ol + lft);
double ob = (fullRect.origin.y + fullRect.size.height), ib = (ob - btm);
double or = (fullRect.origin.x + fullRect.size.width), ir = (or - rgt);
MKMapPoint points[11] = {{ol,it}, {ol,ot}, {or,ot}, {or,ob}, {ol,ob}, {ol,it}, {il,it}, {ir,it}, {ir,ib}, {il,ib}, {il,it}};
MKPolygon *polygon = [MKPolygon polygonWithPoints:points count:11];
}
And my question is now how do I get the minimum number of MKMapRects from this MKPolygon? I have done some googling as well as looking through the forum but havn't found anything.
EDIT:
So the goal is the following:
I have a MKMapRect rect1, then I have a list of rectangles, rectList, which is MKMapRects intersecting with rect1 and what I want to do is create a rectilinear MKPolygon of rect1, remove the surface of all MKMapRects in rectList from rect1 and then create the minimum number of MKMaprects from the created rectilinear MKPolygon.
Right now the problem is the following: I am able to create a polygon when removing one MKMapRect from rect1 but I dont know how to remove the following maprects from rect1 and I dont know how to extract the minimum set of MkMapRects from the polygon created.
Best regards
Peep
I'm not sure if this is what you're looking for or if I understand the question fully, but if all you need to know is the minimum number of rectangles in a polygon that's created by subtracting one rectangle from another you should be able to do it by checking the number of corner points in the second rectangle that are contained in the first rectangle. In pseudo code:
int minNumRects(MKRect r1, MKRect r2) {
int numPointsContained = 0;
for (Point p in r2) {
if (MKMapRectContainsPoint(r1, p)) {
numPointsContained++;
}
}
if (numPointsContained == 1) {
return 2;
} else if (numPointsContained == 2) {
return 3;
} else if (numPointsContained == 4) {
return 4;
} else {
return 0;
}
}
P.S. - This assumes that the rectangles are axis-aligned but as far as I know that's the case with MKRects

Bézier Curve Didn't Draw Straight

I did make algorithm for creating Bézier Curve with Objective-C and Cocos2D. Here is my code
-(int)factorial:(int)x{
int sum=1;
int i;
if(x == 0){
return 1;
}else{
for(i=1;i<x;i++){
sum = sum*i;
}
return sum;
}
}
-(int)binomialCoefficient:(int)n:(int)i{
int sum;
//NSLog([NSString stringWithFormat:#"fac n-i=%f\n", fach] );
sum = [self factorial:n]/([self factorial:i]*[self factorial:(n-i)]);
return sum;
}
-(float)convertT:(int)t{
return t*(0.001);
}
-(float)power:(float)a:(int)b{
int i;
float hasil=1;
for(i=0;i<b;i++){
hasil = hasil*a;
}
return hasil;
}
-(float)bernstein:(float)t:(int)n:(int)i{
float sum = 0;
sum = [self binomialCoefficient:n:i]*[self power:t :i]*[self power:(1-t) :(n-i)];
//NSLog([NSString stringWithFormat:#"yeah"]);
return sum;
}
and for implementation you just put an array of x and y and access it. For example to draw a single dot in control curve I did it like this
float myPx = px[i];
float myPy = py[i];
posx = posx+([self bernstein:theT :banyak-1 :i]*myPx);
posy = posy+([self bernstein:theT :banyak-1 :i]*myPy);
Yes, this code doesn't give the perfect nice line, but I try to draw it dot by dot.
It works well, but the problem arise when I try to use 3 dots. The middle dot for curving the lines didn't behave like what I expected. For example if I put 3 dots in these coordinates:
a(100,200)
b(250,250)
c(500,200)
It didn't curving up but curving down. If I want to put it straight I have to put it all the way higher.
Am I do it wrong in syntax or data types? Or is it just my algorithm?
Thanks in advance
Best Regards
(sorry for my bad english)
The factorial loop should be
for ( i = 1 ; i <= x ; i++ )
instead of
for ( i = 1 ; i < x ; i++ )

How to program smooth movement with the accelerometer like a labyrinth game on iPhone OS?

I want to be able to make image move realistically with the accelerometer controlling it, like any labyrinth game. Below shows what I have so far but it seems very jittery and isnt realistic at all. The ball images seems to never be able to stop and does lots of jittery movements around everywhere.
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
deviceTilt.x = 0.01 * deviceTilt.x + (1.0 - 0.01) * acceleration.x;
deviceTilt.y = 0.01 * deviceTilt.y + (1.0 - 0.01) * acceleration.y;
}
-(void)onTimer {
ballImage.center = CGPointMake(ballImage.center.x + (deviceTilt.x * 50), ballImage.center.y + (deviceTilt.y * 50));
if (ballImage.center.x > 279) {
ballImage.center = CGPointMake(279, ballImage.center.y);
}
if (ballImage.center.x < 42) {
ballImage.center = CGPointMake(42, ballImage.center.y);
}
if (ballImage.center.y > 419) {
ballImage.center = CGPointMake(ballImage.center.x, 419);
}
if (ballImage.center.y < 181) {
ballImage.center = CGPointMake(ballImage.center.x, 181);
}
Is there some reason why you can not use the smoothing filter provided in response to your previous question: How do you use a moving average to filter out accelerometer values in iPhone OS ?
You need to calculate the running average of the values. To do this you need to store the last n values in an array, and then push and pop values off the array when ever you read the accelerometer data. Here is some pseudocode:
const SIZE = 10;
float[] xVals = new float[SIZE];
float xAvg = 0;
function runAverage(float newX){
xAvg += newX/SIZE;
xVals.push(newX);
if(xVals.length > SIZE){
xAvg -= xVals.pop()/SIZE;
}
}
You need to do this for all three axis. Play around with the value of SIZE; the larger it is, the smoother the value, but the slower things will seem to respond. It really depends on how often you read the accelerometer value. If it is read 10 times per second, then SIZE = 10 might be too large.