0

我需要计算绘图上 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_access 永远不会发生

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
4

1 回答 1

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;
于 2012-07-04T00:58:45.883 回答