Keep a fixed-size array of line segments wide enough to accommodate your camera at max zoom. These are fed into your
Vector2 p1; //this will be the same as p0 of the next segment
LineSegment lineSegments = new LineSegment[SIZE];
As you move right, you need to get the appropriate
LineSegment inserted to the start or end of the array (depending on which way you are moving) and shift out old ones (leftmost):
lineSegments = null; //remove leftmost (you don't actually need this line)
//shift all 1 segments to the left
for (int i = 1; i < lineSegments.length; i++)
lineSegments[i - 1] = lineSegments[i];
//get the new rightmost segment
lineSegments[lineSegments.length - 1] = RetrieveLineSegmentAt(i);
//move left - I'll leave this one to you! very similar.
void RetrieveLineSegmentAt(int i)
//retrieve from some larger list, database, or whatever.
If you ensure that your line segments are always the same width in
x (even if tilted), you will always know exactly how many elements you need to fill the screen. I'd recommend this - it's easier. If, however, the rotation of individual line segment causes some to be narrower than others, like so:
5 wide 10 wide
...then you will have to dynamically calculate how many you will need to fill the screen.
I would implement the whole terrain as a 1D heightmap, that is, just a sequence of heights at each evenly-spaced point. Your screen's
lineSegments array can then be populated from the heightmap. This also means you can avoid dynamic width calculation, keeping things simple.