XY Coords를 사용하여 코드로 건조를 어떻게 따라가나요?
-
05-09-2019 - |
문제
이 샘플에서 당신이 선택한 언어로 꿈의 건조 이상을 어떻게 얻을 수 있습니까?
drawLine(Point(0, 0), Point(w, 0));
int curRowY = 0;
for(int row=0; row<rowHeights.size(); row++) {
curRowY += rowHeights[row];
drawLine(Point(0, curRowY), Point(w, curRowY));
}
drawLine(Point(0, 0), Point(0, h));
int curColX = 0;
for(int col=0; col<colWidths.size(); col++) {
curColX += colWidths[col];
drawLine(Point(curColX, 0), Point(curColX, h));
}
참고 : 수많은 임시 전처리기 매크로가 극도로 더 적은 읽기 쉽고 쓰기 쉬워서 나옵니다.
해결책
대답은 간단합니다 : 벡터. 예를 들어
repeatLines(Point start, Point end, Vector direction, int[] gaps)
{
drawLine(start, end);
for (int i = 0; i < gaps.Length; i++)
{
Vector vector = direction * gaps[i];
start += vector;
end += vector;
drawLine(start, end);
}
}
repeatLines(Point(0, 0), Point(0, w), Vector(1, 0), rowHeights);
repeatLines(Point(0, 0), Point(h, 0), Vector(0, 1), colWidths);
다른 팁
스튜어트에 동의하지만 학업 운동으로 압박합니다.
교활한...
어떤면에서, 당신은 그렇지 않습니다 진짜 자신을 반복; 당신은 서로 (문자 그대로, 비 유적으로) 서로 직교 한 두 가지 비슷한 일을하고 있습니다.
나는 당신이 다음을 수행 할 수 있다고 생각하지만, 더 이상 읽을 수없고 더 이상 성능이 없다고 생각합니다.
의사 C#] :
void DrawGrid()
{
DrawLines(w, rowHeights, true);
DrawLines(h, colWidths, false);
}
void DrawLines(int lineLength, int[] lineSeparations, bool isHorizontal)
{
MyDrawLine(Point(0, 0), Point(lineLength, 0), isHorizontal);
int offset = 0;
for (int i = 0; i < widths.length; i++)
{
offset += lineSeparations[i];
MyDrawLine(Point(offset, 0), Point(offset, lineLength), isHorizontal);
}
}
void MyDrawLine(Point startPoint, Point endPoint, bool isHorizontal)
{
if (isHorizontal)
{
SwapXAndYCoordinates(startPoint);
SwapXAndYCoordinates(endPoint);
}
drawLine(startPoint, endPoint);
}
두 번째 생각에서, 나는 이것이 바보 같은 생각이라고 생각합니다 ... :-)
어쩌면이 경우 극단적으로 너무 많이 건조하고 있습니까?
그럼에도 불구하고, 학문적 운동으로서, 나는 누군가가 당신의 현재 코드만큼 읽을 수있는 솔루션을 만들 수 있지만 명백한 복제가없는 솔루션을 생각해 낼 수 있는지를 기대하고 있습니다!
그리드가 정사각형이라면 다음이 작동 할 수 있다고 생각합니다.
void drawGrid()
{
for(int i = 1, offset = 10; i <= numPoints; i++, offset += 10)
{
Point p = new Point(i * offset, i * offset);
drawHorizontal(p);
drawVertical(p);
}
}
void drawHorizontal(Point p)
{
drawLine(new Point(0, p.y), new Point(width, p.y));
}
void drawVertical(Point p)
{
drawLine(new Point(p.x, 0), new Point(p.x, height));
}
제휴하지 않습니다 StackOverflow