一个直线的斜率【难度:1级】:
答案1:
namespace Solution
{
using NUnit.Framework;
using System;
using System.Collections.Generic;
public static class Solution
{
public static double? GetSlope(Point p1, Point p2)
{
if (p1.X == p2.X) {
return null; }
return (p1.Y - p2.Y) / (p1.X - p2.X);
}
}
[TestFixture, Description("Tests")]
public class GetSlopeTest
{
private static IEnumerable<TestCaseData> testCases
{
get
{
yield return new TestCaseData(new Point(1, 1), new Point(2, 2)).Returns(1).SetDescription("Should calculate the existing non-zero between 2 points");
yield return new TestCaseData(new Point(-5,-5), new Point(9,9)).Returns(1).SetDescription("Should calculate the existing non-zero between 2 points");
yield return new TestCaseData(new Point(1,8), new Point(2,9)).Returns(1).SetDescription("Should calculate the existing non-zero between 2 points");
yield return new TestCaseData(new Point(8,3), new Point(-4,5)).Returns(-1/6.0).SetDescription("Should calculate the existing non-zero between 2 points");
yield return new TestCaseData(new Point(5,3), new Point(8,9