本文介绍了在iOS 5.0设备中显示复杂的数学方程式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为iPhone和iPod Touch开发一个应用程序,该应用程序必须显示复杂的数学方程式,如代数,积分,求和公式以及一些文本.
为此,我使用了Quartz2D,下面是我创建的内容.任何人都可以验证这是否是绘制方程式的正确程序吗?
我必须显示很多带有内联方程的文本(而且过于动态)-我确信使用此过程将是使该事物动态化的任务.
I am working on an app for iPhone and iPod Touch that has to show complex mathematical equations like algebraic, integration, summation formulas along with some text.
For that I have used Quartz2D and below is what I have created. Can anyone please verify if this is the correct procedure to draw an equation?
I have to show lots of text with inline equations (and that too dynamic) - I am sure using this procedure will be a task to make that thing dynamic.
- (void)drawRect:(CGRect)rect
{
// Left hand side Starts --
float xaxis = 3.0f;
// Before bracket
NSString *textString = @"L";
[textString drawAtPoint:CGPointMake(xaxis, 50.) withFont:[UIFont fontWithName:@"Helvetica-Oblique" size:20]];
// Opening bracket
xaxis += 12.0f;
textString = @"(";
[textString drawAtPoint:CGPointMake(xaxis, 30.) withFont:[UIFont fontWithName:@"Helvetica" size:50]];
textString = @"N";
xaxis += 12.0f;
[textString drawAtPoint:CGPointMake(xaxis, 50.) withFont:[UIFont fontWithName:@"Helvetica-Oblique" size:20]];
// Numerator part
unichar oneChar[] = {0x2206};
xaxis += 31.0f;
textString = getTextString(oneChar, 1);
[textString drawAtPoint:CGPointMake(xaxis, 37.) withFont:[UIFont fontWithName:@"Helvetica" size:22]];
unichar twoChar[] = {0x03C9};
xaxis += 15.0f;
textString = getTextString(twoChar, 1);
[textString drawAtPoint:CGPointMake(xaxis, 38.) withFont:[UIFont fontWithName:@"Helvetica" size:20]];
xaxis += 15.0f;
textString = @"k";
[textString drawAtPoint:CGPointMake(xaxis, 48.) withFont:[UIFont fontWithName:@"Helvetica" size:12]];
// Line
CGContextRef myContext = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(myContext, 2);
CGContextSetStrokeColorWithColor(myContext, [UIColor blackColor].CGColor);
CGContextMoveToPoint(myContext, 45, 65);
CGContextAddLineToPoint(myContext, 110, 65);
CGContextStrokePath(myContext);
// Denominator part
xaxis -= 40.0f;
unichar threeChar[] = {0x03C9};
textString = getTextString(threeChar, 1);
[textString drawAtPoint:CGPointMake(xaxis, 64.) withFont:[UIFont fontWithName:@"Helvetica" size:20]];
xaxis += 15.0f;
textString = @"res";
[textString drawAtPoint:CGPointMake(xaxis, 74.) withFont:[UIFont fontWithName:@"Helvetica" size:12]];
xaxis += 18.0f;
unichar fourChar[] = {0x0028};
textString = getTextString(fourChar, 1);
[textString drawAtPoint:CGPointMake(xaxis, 64.) withFont:[UIFont fontWithName:@"Helvetica" size:20]];
xaxis += 5.0f;
unichar fiveChar[] = {0x03B8};
textString = getTextString(fiveChar, 1);
[textString drawAtPoint:CGPointMake(xaxis, 64.) withFont:[UIFont fontWithName:@"Helvetica-Oblique" size:20]];
xaxis += 15.0f;
textString = @")";
[textString drawAtPoint:CGPointMake(xaxis, 64.) withFont:[UIFont fontWithName:@"Helvetica" size:20]];
// Closing bracket
xaxis += 8.0f;
textString = @")";
[textString drawAtPoint:CGPointMake(xaxis, 30.) withFont:[UIFont fontWithName:@"Helvetica" size:50]];
// Equal to sign
xaxis += 15.0f;
unichar sixChar[] = {0x003D};
textString = getTextString(sixChar, 1);
[textString drawAtPoint:CGPointMake(xaxis, 50.) withFont:[UIFont fontWithName:@"Helvetica" size:20]];
// Right hand side Starts --
// Numerator part
xaxis += 15.0f;
textString = @"sin";
[textString drawAtPoint:CGPointMake(xaxis, 42.) withFont:[UIFont fontWithName:@"Helvetica" size:17]];
xaxis += 20.0f;
textString = @"2";
[textString drawAtPoint:CGPointMake(xaxis, 38.) withFont:[UIFont fontWithName:@"Helvetica" size:12]];
// Inner opening bracket
xaxis += 10.0f;
textString = @"(";
[textString drawAtPoint:CGPointMake(xaxis, 37.) withFont:[UIFont fontWithName:@"Helvetica" size:22]];
xaxis += 8.0f;
textString = @"N";
[textString drawAtPoint:CGPointMake(xaxis, 38.) withFont:[UIFont fontWithName:@"Helvetica-Oblique" size:20]];
xaxis += 18.0f;
unichar sevenChar[] = {0x03C0};
textString = getTextString(sevenChar, 1);
[textString drawAtPoint:CGPointMake(xaxis, 40.) withFont:[UIFont fontWithName:@"Helvetica" size:17]];
xaxis += 12.0f;
unichar eightChar[] = {0x2206};
textString = getTextString(eightChar, 1);
[textString drawAtPoint:CGPointMake(xaxis, 36.) withFont:[UIFont fontWithName:@"Helvetica" size:21]];
xaxis += 15.0f;
unichar nineChar[] = {0x03C9};
textString = getTextString(nineChar, 1);
[textString drawAtPoint:CGPointMake(xaxis, 38.) withFont:[UIFont fontWithName:@"Helvetica" size:20]];
xaxis += 15.0f;
textString = @"k";
[textString drawAtPoint:CGPointMake(xaxis, 47.) withFont:[UIFont fontWithName:@"Helvetica" size:12]];
xaxis += 8.0f;
textString = @"/";
[textString drawAtPoint:CGPointMake(xaxis, 38.) withFont:[UIFont fontWithName:@"Helvetica" size:24]];
xaxis += 8.0f;
unichar tenChar[] = {0x03C9};
textString = getTextString(tenChar, 1);
[textString drawAtPoint:CGPointMake(xaxis, 38.) withFont:[UIFont fontWithName:@"Helvetica" size:20]];
xaxis += 15.0f;
textString = @"res";
[textString drawAtPoint:CGPointMake(xaxis, 48.) withFont:[UIFont fontWithName:@"Helvetica" size:12]];
xaxis += 18.0f;
textString = @"(";
[textString drawAtPoint:CGPointMake(xaxis, 38.) withFont:[UIFont fontWithName:@"Helvetica" size:20]];
xaxis += 5.0f;
unichar elevenChar[] = {0x03B8};
textString = getTextString(elevenChar, 1);
[textString drawAtPoint:CGPointMake(xaxis, 38.) withFont:[UIFont fontWithName:@"Helvetica-Oblique" size:20]];
xaxis += 12.0f;
textString = @")";
[textString drawAtPoint:CGPointMake(xaxis, 38.) withFont:[UIFont fontWithName:@"Helvetica" size:20]];
// Inner closing bracket
xaxis += 6.0f;
textString = @")";
[textString drawAtPoint:CGPointMake(xaxis, 37.) withFont:[UIFont fontWithName:@"Helvetica" size:22]];
// Line
CGContextSetLineWidth(myContext, 2);
CGContextSetStrokeColorWithColor(myContext, [UIColor blackColor].CGColor);
CGContextMoveToPoint(myContext, 138, 65);
CGContextAddLineToPoint(myContext, 318, 65);
CGContextStrokePath(myContext);
// Denominator part
xaxis -= 175.0f;
textString = @"N";
[textString drawAtPoint:CGPointMake(xaxis, 65.) withFont:[UIFont fontWithName:@"Helvetica-Oblique" size:20]];
xaxis += 18.0f;
textString = @"2";
[textString drawAtPoint:CGPointMake(xaxis, 63.) withFont:[UIFont fontWithName:@"Helvetica" size:12]];
xaxis += 8.0f;
textString = @"sin";
[textString drawAtPoint:CGPointMake(xaxis, 68.) withFont:[UIFont fontWithName:@"Helvetica" size:17]];
xaxis += 20.0f;
textString = @"2";
[textString drawAtPoint:CGPointMake(xaxis, 63.) withFont:[UIFont fontWithName:@"Helvetica" size:12]];
// Inner opening bracket
xaxis += 10.0f;
textString = @"(";
[textString drawAtPoint:CGPointMake(xaxis, 64.) withFont:[UIFont fontWithName:@"Helvetica" size:22]];
xaxis += 8.0f;
unichar twelveChar[] = {0x03C0};
textString = getTextString(twelveChar, 1);
[textString drawAtPoint:CGPointMake(xaxis, 68.) withFont:[UIFont fontWithName:@"Helvetica" size:17]];
xaxis += 12.0f;
unichar thirteenChar[] = {0x2206};
textString = getTextString(thirteenChar, 1);
[textString drawAtPoint:CGPointMake(xaxis, 65.) withFont:[UIFont fontWithName:@"Helvetica" size:21]];
xaxis += 15.0f;
unichar fourteenChar[] = {0x03C9};
textString = getTextString(fourteenChar, 1);
[textString drawAtPoint:CGPointMake(xaxis, 67.) withFont:[UIFont fontWithName:@"Helvetica" size:20]];
xaxis += 15.0f;
textString = @"k";
[textString drawAtPoint:CGPointMake(xaxis, 75.) withFont:[UIFont fontWithName:@"Helvetica" size:12]];
xaxis += 8.0f;
textString = @"/";
[textString drawAtPoint:CGPointMake(xaxis, 64.) withFont:[UIFont fontWithName:@"Helvetica" size:24]];
xaxis += 8.0f;
unichar fifteenChar[] = {0x03C9};
textString = getTextString(fifteenChar, 1);
[textString drawAtPoint:CGPointMake(xaxis, 67.) withFont:[UIFont fontWithName:@"Helvetica" size:20]];
xaxis += 15.0f;
textString = @"res";
[textString drawAtPoint:CGPointMake(xaxis, 75.) withFont:[UIFont fontWithName:@"Helvetica" size:12]];
xaxis += 18.0f;
textString = @"(";
[textString drawAtPoint:CGPointMake(xaxis, 66.) withFont:[UIFont fontWithName:@"Helvetica" size:20]];
xaxis += 5.0f;
unichar sixteenChar[] = {0x03B8};
textString = getTextString(sixteenChar, 1);
[textString drawAtPoint:CGPointMake(xaxis, 66.) withFont:[UIFont fontWithName:@"Helvetica-Oblique" size:20]];
xaxis += 12.0f;
textString = @")";
[textString drawAtPoint:CGPointMake(xaxis, 66.) withFont:[UIFont fontWithName:@"Helvetica" size:20]];
// Inner closing bracket
xaxis += 6.0f;
textString = @")";
[textString drawAtPoint:CGPointMake(xaxis, 64.) withFont:[UIFont fontWithName:@"Helvetica" size:22]];
// Extra text for information
xaxis = 6.0f;
textString = @"Above is the example of an equation drawn using";
[textString drawAtPoint:CGPointMake(xaxis, 120.) withFont:[UIFont fontWithName:@"Helvetica" size:14]];
xaxis = 6.0f;
textString = @"Quartz2D";
[textString drawAtPoint:CGPointMake(xaxis, 140.) withFont:[UIFont fontWithName:@"Helvetica" size:14]];
}
static NSString *getTextString(unichar chars[], int charLength)
{
NSString *uniString = [NSString stringWithCharacters:chars length: charLength];
return uniString;
}
推荐答案
<html>
<head>
<title>MathML sample</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h2>MathML sample:</h2>
<p><big><big>
<math mode="display" xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<mi>x</mi>
<mo>=</mo>
<mfrac>
<mrow>
<mo form="prefix">−<!-- − --></mo>
<mi>b</mi>
<mo>±<!-- ± --></mo>
<msqrt>
<msup>
<mi>b</mi>
<mn>2</mn>
</msup>
<mo>−<!-- − --></mo>
<mn>4</mn>
<mo>⁢<!-- ⁢ --></mo>
<mi>a</mi>
<mo>⁢<!-- ⁢ --></mo>
<mi>c</mi>
</msqrt>
</mrow>
<mrow>
<mn>2</mn>
<mo>⁢<!-- ⁢ --></mo>
<mi>a</mi>
</mrow>
</mfrac>
</mrow>
</math>
</big></big></p>
</body>
</html>
这篇关于在iOS 5.0设备中显示复杂的数学方程式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!