计算图上2点之间的距离

时间:2012-07-03 14:33:05

标签: core-plot

我需要计算图上2点之间的距离。

我正在使用以下方法来满足这种需求。我尝试了其他方法,但这是唯一一个实际情节。在其他方法上,它总是返回0。

-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index 


  NSDecimal plotPoint[2];
        CPTPlotSpace *thePlotSpace = plot.plotSpace;
        CPTPlotArea *thePlotArea   = plot.plotArea;
        CPTCoordinate independentCoord = (NO ? CPTCoordinateY : CPTCoordinateX);

        plotPoint[independentCoord] = [plot cachedDecimalForField:CPTBarPlotFieldBarLocation recordIndex:0];

        CGPoint basePoint;

        basePoint = [plot convertPoint:[thePlotSpace plotAreaViewPointForPlotPoint:plotPoint] fromLayer:thePlotArea];

        plotPoint[independentCoord] = [plot cachedDecimalForField:CPTBarPlotFieldBarLocation recordIndex:1];

        CGPoint basePoint2;

        basePoint2 = [plot convertPoint:[thePlotSpace plotAreaViewPointForPlotPoint:plotPoint] fromLayer:thePlotArea];


        int valor; 
        valor =  basePoint2.x - basePoint.x;

上面的代码几乎总是有效,但我得到的EXC_BAD_ACESS来自上面的代码。

当我评论代码时,exc_bad_acess永远不会发生

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x00000004
Crashed Thread:  0

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   Foundation                      0x32f7be56 NSIntegerDivide + 98
1   Foundation                      0x32f7f9e0 NSDecimalDivide + 216
2   InfoTOS007                      0x000e198c CPTDecimalDivide (CPTUtilities.m:496)
3   InfoTOS007                      0x000fd0c8 -[CPTXYPlotSpace     viewCoordinateForViewLength:linearPlotRange:plotCoordinateValue:] (CPTXYPlotSpace.m:384)
4   InfoTOS007                      0x000fd66a -[CPTXYPlotSpace plotAreaViewPointForPlotPoint:] (CPTXYPlotSpace.m:449)
5   InfoTOS007                      0x000bea38 -[GraficoEmbarqueDescarga dataLabelForPlot:recordIndex:] (GraficoEmbarqueDescarga.m:249)
6   InfoTOS007                      0x000ce192 -[CPTPlot relabel] (CPTPlot.m:1016)
7   InfoTOS007                      0x000cb868 -[CPTPlot layoutSublayers] (CPTPlot.m:402)
8   QuartzCore                      0x34272f92 CA::Layer::layout_if_needed(CA::Transaction*) + 210
9   QuartzCore                      0x34277114 CA::Context::commit_transaction(CA::Transaction*) + 220
10  QuartzCore                      0x34276e50 CA::Transaction::commit() + 308
11  QuartzCore                      0x3426ed7e CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 50
12  CoreFoundation                  0x344deb44 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 12
13  CoreFoundation                  0x344dcd80 __CFRunLoopDoObservers + 252
14  CoreFoundation                  0x344dd0da __CFRunLoopRun + 754
15  CoreFoundation                  0x344604d6 CFRunLoopRunSpecific + 294
16  CoreFoundation                  0x3446039e CFRunLoopRunInMode + 98
17  GraphicsServices                0x30d4bfc6 GSEventRunModal + 150
18  UIKit                           0x32a2873c UIApplicationMain + 1084
19  InfoTOS007                      0x000b3468 main (main.m:16)
20  InfoTOS007                      0x000b340c start + 32

1 个答案:

答案 0 :(得分:2)

看起来问题是你从不设置plotPoint [dependentCoord],所以它试图将未定义的值转换为绘图坐标。只需将其设置为图表范围内的值,如果您担心的是X轴,那么Y轴值无关紧要。我没有尝试编译以下内容,但它看起来是正确的。

  NSDecimal plotPoint[2];
  CPTPlotSpace *thePlotSpace = plot.plotSpace;
  CPTPlotArea *thePlotArea   = plot.plotArea;
  CPTCoordinate independentCoord =  CPTCoordinateX;
  CPTCoordinate dependentCoord =  CPTCoordinateY;

  plotPoint[independentCoord] = [plot cachedDecimalForField:CPTBarPlotFieldBarLocation recordIndex:0];
  plotPoint[dependentCoord] = CPTDecimalFromInt(1);

  CGPoint basePoint;
  basePoint = [plot convertPoint:[thePlotSpace plotAreaViewPointForPlotPoint:plotPoint] fromLayer:thePlotArea];

  plotPoint[independentCoord] = [plot cachedDecimalForField:CPTBarPlotFieldBarLocation recordIndex:1];
   CGPoint basePoint2;
   basePoint2 = [plot convertPoint:[thePlotSpace plotAreaViewPointForPlotPoint:plotPoint] fromLayer:thePlotArea];

    int valor; 
    valor =  basePoint2.x - basePoint.x;