Liang-Barsky

Liang-Barsky

In computer graphics, the Liang-Barsky algorithm is a line clipping algorithm. The Liang-Barsky algorithm uses the parametric equation of a line and inequalities describing the range of the clipping box to determine the intersections between the line and the clipping box. With these intersections it knows which portion of the line should be drawn. This algorithm is significantly more efficient than Cohen-Sutherland.

The algorithm

C# implementation for Liang-Barsky algorithminternal sealed class LiangBarskyClipping : IClippingAlgorithm { private Vector2 _clipMin, _clipMax;

public IEnumerable GetBoundingPolygon() { yield return _clipMin; yield return new Vector2(_clipMax.X, _clipMin.Y); yield return _clipMax; yield return new Vector2(_clipMin.X, _clipMax.Y); }

public void SetBoundingRectangle(Vector2 start, Vector2 end) { _clipMin = start; _clipMax = end; }

public void SetBoundingPolygon(IEnumerable points) { throw new NotSupportedException("see Capabilities =)"); }

private delegate bool ClippingHandler(float p, float q);

public bool ClipLine(ref Line line) { Vector2 P = line.End - line.Start; float tMinimum = 0, tMaximum = 1;

ClippingHandler pqClip = delegate(float directedProjection, float directedDistance) { if (directedProjection = 0) { if (directedDistance < 0) return false; } else { float amount = directedDistance / directedProjection; if (directedProjection < 0) { if (amount > tMaximum) return false; else if (amount > tMinimum) tMinimum = amount; } else { if (amount < tMinimum) return false; else if (amount < tMaximum) tMaximum = amount; } } return true; };

if (pqClip(-P.X, line.Start.X - _clipMin.X)) { if (pqClip(P.X, _clipMax.X - line.Start.X)) { if (pqClip(-P.Y, line.Start.Y - _clipMin.Y)) { if (pqClip(P.Y, _clipMax.Y - line.Start.Y)) { if (tMaximum < 1) { line.End.X = line.Start.X + tMaximum * P.X; line.End.Y = line.Start.Y + tMaximum * P.Y; } if (tMinimum > 0) { line.Start.X += tMinimum * P.X; line.Start.Y += tMinimum * P.Y; } return true; } } } } return false; }

public ClippingCapabilities Capabilities { get { return ClippingCapabilities.RectangleWindow; } }

public override string ToString() { return "Liang-Barsky algorithm"; // This code was implemented by Grishul Eugeny as part of preparation// to exam in ITMO university

ee also

Algorithms used for the same purpose:
* Cyrus-Beck
* Nicholl-Lee-Nicholl
* Fast-Clipping


Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • Algoritmo de Liang-Barsky — El algoritmo de Liang Barsky es un algoritmo de recorte de líneas similar al algoritmo de Cohen Sutherland. Usa la ecuación paramétrica de la línea y desigualdades describiendo el rango del área de recorte para determinar las intersecciones entre …   Wikipedia Español

  • Line clipping — In computer graphics, line clipping is the process of removing lines or portions of lines outside of an area of interest. Typically, any line or part thereof which is outside of the viewing area is removed.There are two common algorithms for line …   Wikipedia

  • Clipping (computer graphics) — Any procedure which identifies that portion of a picture which is either inside or outside a picture is referred to as a clipping algorithm or clipping. The region against which an object is to be clipped is called clipping window. Contents 1… …   Wikipedia

  • Cohen-Sutherland — In computer graphics, the Cohen Sutherland algorithm is a line clipping algorithm. The algorithm divides a 2D space into 9 parts, of which only the middle part (viewport) is visible. The algorithmThe algorithm includes, excludes or partially… …   Wikipedia

  • Nicholl-Lee-Nicholl — is one of computer graphics line clipping algorithms. It uses symmetry to categorize endpoints into one of three regions (in the clip window, edge, and corner). Then according to endpoint category it checks several clip cases.For the point we… …   Wikipedia

  • Clipping (Computergrafik) — Als Clipping oder Abschneiden (englisch to clip = „abschneiden“, „kappen“) bezeichnet man in der Computergrafik das Abschneiden von Grundobjekten am Rand eines gewünschten Bildschirmausschnittes oder Fensters. Ein Fenster kann dabei ein… …   Deutsch Wikipedia

  • Algoritmo de Cohen-Sutherland — El algoritmo de Cohen Sutherland es un algoritmo de recorte de líneas usado en gráficos por computadora. Fue desarrollado por Danny Cohen e Ivan Sutherland en 1967. Contenido 1 Introducción 2 Funcionamiento 2.1 Códigos de frontera …   Wikipedia Español

  • Recorte de líneas — Saltar a navegación, búsqueda En informática gráfica el recorte de líneas, es el proceso de quitar las líneas o porciones de ellas que estén fuera de un área de interés. Normalmente cualquier línea o una parte es quitada si está fuera del área de …   Wikipedia Español

  • Список алгоритмов — Эта страница информационный список. Основная статья: Алгоритм Ниже приводится список алгоритмов, группированный по категориям. Более детальные сведения приводятся в списке структур данных и …   Википедия

  • Отсечение — или клиппинг (англ. clipping) метод оптимизации в рендеринге и компьютерной графике, когда компьютер прорисовывает только ту часть сцены, которая может находиться в поле зрения пользователя. В двухмерной графике, если пользователь увеличил… …   Википедия

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”