Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (* Data Input *)
- PointListx = {};
- PointListy = {};
- LenPointList = {};
- (* Seg 1 *)
- AppendTo[PointListx, {{0, 6, 0}, {0, 8, 18}, {18, 20}, {0, 18}, {20,
- 20}, {20, 17, 13}, {11, 6, 0}}];
- AppendTo[PointListy, {{20, 15, 10}, {20, 23, 17}, {17, 15}, {10,
- 17}, {0, 25}, {0, 3, 4}, {3, 0, 4}}];
- AppendTo[LenPointList, Length[PointListx[[1]]]];
- (* Seg 2 *)
- AppendTo[PointListx, {{38, 40.5, 38}, {38, 33, 28}, {38, 35, 32}, {38,
- 35, 32}}];
- AppendTo[PointListy, {{22, 14, 5}, {5, 0, 5}, {22, 19, 22}, {22, 25,
- 22}}];
- AppendTo[LenPointList, Length[PointListx[[2]]]];
- (* Seg 3 *)
- AppendTo[PointListx, {{45, 42, 45}, {45, 47, 45}}];
- AppendTo[PointListy, {{25, 19, 10}, {10, 5, 2}}];
- AppendTo[LenPointList, Length[PointListx[[3]]]];
- (* Seg 4 *)
- AppendTo[PointListx, {{50, 58, 50}, {50, 61, 67}, {67, 67}}];
- AppendTo[PointListy, {{25, 16.5, 7}, {7, 7, 0}, {0, 25}}];
- AppendTo[LenPointList, Length[PointListx[[4]]]];
- (* Seg 5 *)
- AppendTo[PointListx, {{20, 70}}];
- AppendTo[PointListy, {{25, 25}}];
- AppendTo[LenPointList, Length[PointListx[[5]]]];
- (* End Seg *)
- NoSubSegment = Length[LenPointList];
- (* Working Section *)
- (* Most Outer Loop, Works for the segments *)
- For[Loop1 = 1, Loop1 <= NoSubSegment, Loop1++,
- List1x = PointListx[[Loop1]];
- List1y = PointListy[[Loop1]];
- Len1 = Length[List1x];
- ipol = 0;
- (* Second Outer Loop. Works for the points inside a segment *)
- For[Loop2 = 1, Loop2 <= Len1, Loop2++,
- List2x = List1x[[Loop2]];
- List2y = List1y[[Loop2]];
- Len2 = Length[List2x];
- (* Checking vertical or horizontal line *)
- If[Len2 == 2,
- If[List2x[[2]] - List2x[[1]] == 0,
- Print["x = ", List2x[[1]], " {", Sort[List2y][[1]] ,
- "\[LessEqual]y\[LessEqual]", Sort[List2y][[2]], "}"]; ipol = 2,
- If[List2y[[2]] - List2y[[1]] == 0,
- Print["y = ", List2y[[1]], " {", Sort[List2x][[1]] ,
- "\[LessEqual]x\[LessEqual]", Sort[List2x][[2]], "}"]; ipol = 2]
- ]
- ];
- (* Checking orientation of Parabola *)
- If[Len2 == 3,
- If[(List2x[[2]] - List2x[[1]])*(List2x[[2]] - List2x[[3]]) >= 0,
- ipol = 1]
- ];
- (* Main Interpolation Code Begins *)
- If[ipol != 2,
- PolynomialMatrix =
- Join[ConstantArray[0, {Len2 - 1, Len2}],
- ConstantArray[1, {1, Len2}]];
- (* y = ax^2 + bx + c type parabola *)
- If[ipol == 0,
- For[i = 1, i <= (Len2 - 1), i++,
- For[j = 1, j <= Len2 , j++,
- PolynomialMatrix[[i, j]] = (List2x[[j]] ^ (Len2 - i))
- ]
- ];
- Result = LinearSolve[Transpose[PolynomialMatrix], List2y ],
- (* x = ay^2 + by + c type parabola *)
- For[i = 1, i <= (Len2 - 1), i++,
- For[j = 1, j <= Len2 , j++,
- PolynomialMatrix[[i, j]] = (List2y[[j]] ^ (Len2 - i))
- ]
- ];
- Result = LinearSolve[Transpose[PolynomialMatrix], List2x ]
- ];
- TrResult = Transpose[{Result}];
- (* Equation Making *)
- sump = 0;
- prodp = 1;
- If[ipol == 0,
- For[i = 1, i <= Len2, i++,
- prodp = prodp * (x^(i - 1)) * TrResult[[Len2 - i + 1, 1]];
- sump = sump + prodp;
- prodp = 1
- ],
- For[i = 1, i <= Len2, i++,
- prodp = prodp * (y^(i - 1)) * TrResult[[Len2 - i + 1, 1]];
- sump = sump + prodp;
- prodp = 1
- ]
- ];
- F = Simplify[sump];
- (* Output Suffix and Prefix finding *)
- If[ipol == 0,
- pref = "y = ";
- lim = {Sort[List2x][[1]], "\[LessEqual]x\[LessEqual]",
- Sort[List2x][[Len2]]},
- pref = "x =";
- lim = {Sort[List2y][[1]], "\[LessEqual]y\[LessEqual]",
- Sort[List2y][[Len2]]}
- ];
- (* Output *)
- Print[pref, F , " {", lim[[1]], lim[[2]], lim[[3]], "}"]
- ];
- ipol = 0;
- ]
- (* Output to indicate end of a segment *)
- Print["End of Segment ", Loop1]
- ]
Add Comment
Please, Sign In to add comment