Skall rita en linje X1,Y1-X2,Y2 i en ruta som har storleken 0,0-65536,65536.... Gammalt välkänt clipping-problem: Du råkar inte ha skrivit något kod i VB för sådant? Detta är inte C++ utan pseudokod, du kan översätta den till vad som helst. Ok... Fattar typ inget av vad Koden menar... Får la sätta mig mig igen när jag inte är på jobbet och allt inte är så stressigt.. Har tyvärr sök på Google.. Hittar mest en massa om hur du fyller objekt osv.. Men han beskriver ju det i text? Nu har jag redigerat ner det så det blir mer lättläst.... procedure CohenSutherlandLineClipAndDraw(x0,y0,x1,y1,xmin,xmax,ymin,ymax : real ; value: integer); Nej färdig Kod behöver jag inte. Hm...Klurig Mattegrej....
Om jag gör en förflyttning av linjen så att tex ena punkten hamnar utanför rutan så vill jag ha en funktion som räknar om så linjen slutar vid rutans kant och inte inte utanför....
Typ:
RitaLinje(X1,Y1,X2,Y2,R,G,B)
Där R,G,B är färgblandningen på Linjen...
Den Ritar givetvis bara den delen av strecket som är innamför ritområdet...
Är hela strecket utanför så ritar den givetvis inget...Sv: Klurig Mattegrej....
http://www.cc.gatech.edu/grads/h/Hao-wei.Hsieh/Haowei.Hsieh/mm.htmlSv:Klurig Mattegrej....
Bra länk men jag kan inte C++ så jag förstår inte hur de gör...
Eller någon annan som gjort det?Sv: Klurig Mattegrej....
http://www.cc.gatech.edu/grads/h/Hao-wei.Hsieh/Haowei.Hsieh/code1.htmlSv:Klurig Mattegrej....
Om ingetn har ett exempel i VB så klart.. lerSv:Klurig Mattegrej....
Den första länken mi skrev var bra för den tog upp hur man gjorde med streck men jag förstod inte hans exempel.....Sv: Klurig Mattegrej....
Jag förstår inte riktigt vad problemet är? Vill du ha färdig kod?
Du hajar väl att principen är att:
1. Om båda punkterna är utanför på samma sida, rita inte alls;
2. Om båda punkterna är innanför; rita hela.
3. Annars, räkna ut var skärningspunkterna sitter.
Du kanske inte behöver det mest optimala sättet, då kan du göra 1 och 2 först, och sen 3 på det enklast möjliga sättet. Räkna först ut om linjen ligger utanför eller innanför de fyra hörnpunkterna. Går den innanför alla är det sen bara att räkna ut skärningen med de fyra linjerna x= x_min, x=x_max, y=y_min, y=y_max, och ta de två som ligger innerst.Sv:Klurig Mattegrej....
procedure CohenSutherlandLineClipAndDraw(x0,y0,x1,y1,xmin,xmax,ymin,ymax : real ; value: integer);
{Cohen-Sutherland clipping algorithm for line P0=(x0,y0) to P1=(x1,y1) and clip rectangle with diagonal from (xmin,ymin) to (xmax,ymax).}
type
edge = (LEFT,RIGHT,BOTTOM,TOP);
outcode = set of edge;
var
accept,done : boolean;
outcode0,outcode1,outcodeOut : outcode;
{Outcodes for P0,P1, and whichever point lies outside the clip rectangle}
x,y : real;
procedure CompOutCode(x,y: real; var code:outcode);{Compute outcode for the point (x,y) }
begin
code := [];
if y > ymax then code := [TOP]
else if y < ymin then code := [BOTTOM];
if x > xmax then code := code +[RIGHT]
else if x < xmin then code := code +[LEFT]
end;
begin
accept := false; done := false;
CompOutCode (x0,y0,outcode0); CompOutCode (x1,y1,outcode1);
repeat
if(outcode0=[]) and (outcode1=[]) then {Trivial accept and exit}
begin
accept := true; done:=true
end
else if (outcode0*outcode1) <> [] then
done := true {Logical intersection is true, so trivial reject and exit.}
else {Failed both tests, so calculate the line segment to clip; from an outside point to an intersection with clip edge.}
begin {At least one endpoint is outside the clip rectangle; pick it.}
if outcode0 <> [] then outcodeOut := outcode0 else outcodeOut := outcode1; {Now find intersection point; use formulas y=y0+slope*(x-x0),x=x0+(1/slope)*(y-y0).}
if TOP in outcodeOut then
begin {Divide line at top of clip rectangle}
x := x0 + (x1 - x0) * (ymax - y0) / (y1 - y0);
y := ymax
end
if BOTTOM in outcodeOut then
begin {Divide line at bottom of clip rectangle}
x := x0 + (x1 - x0) * (ymin - y0) / (y1 - y0);
y := ymax
end
else if RIGHT in outcodeOut then
begin {Divide line at right edge of clip rectangle}
y := y0 + (y1 - y0) * (xmax - x0) / (x1 - x0);
x := xmax
end
else if LEFT in outcodeOut then
begin {Divide line at left edge of clip rectangle}
y := y0 + (y1 - y0) * (xmin - x0) / (x1 - x0);
x := xmin
end;
{Now we move outside point to intersection point to clip, and get ready for next pass.}
if (outcodeOut = outcode0) then
begin
x0 := x; y0 := y; CompOutCode(x0,y0,outcode0)
end
else
begin
x1 := x; y1 := y; CompOutCode(x1,y1,outcode1);
end
end {subdivide}
until done;
if accept then MidpointLineReal(x0,y0,x1,y1,value) {Version for real coordinates}
end; {CohenSutherlandLineClipAndDraw}
Sv:Klurig Mattegrej....
{Cohen-Sutherland clipping algorithm for line P0=(x0,y0) to P1=(x1,y1) and clip rectangle with diagonal from (xmin,ymin) to (xmax,ymax).}
type
edge = (LEFT,RIGHT,BOTTOM,TOP);
outcode = set of edge;
var
accept,done : boolean;
outcode0,outcode1,outcodeOut : outcode;
{Outcodes for P0,P1, and whichever point lies outside the clip rectangle}
x,y : real;
procedure CompOutCode(x,y: real; var code:outcode);{Compute outcode for the point (x,y) }
begin
code := [];
if y > ymax then code := [TOP]
else if y < ymin then code := [BOTTOM];
if x > xmax then code := code +[RIGHT]
else if x < xmin then code := code +[LEFT]
end;
begin
accept := false; done := false;
CompOutCode (x0,y0,outcode0); CompOutCode (x1,y1,outcode1);
repeat
if(outcode0=[]) and (outcode1=[]) then {Trivial accept and exit}
begin
accept := true; done:=true
end
else if (outcode0*outcode1) <> [] then
done := true {Logical intersection is true, so trivial reject and exit.}
else {Failed both tests, so calculate the line segment to clip; from an outside point to an intersection with clip edge.}
begin {At least one endpoint is outside the clip rectangle; pick it.}
if outcode0 <> [] then outcodeOut := outcode0 else outcodeOut := outcode1; {Now find intersection point; use formulas y=y0+slope*(x-x0),x=x0+(1/slope)*(y-y0).}
if TOP in outcodeOut then
begin {Divide line at top of clip rectangle}
x := x0 + (x1 - x0) * (ymax - y0) / (y1 - y0);
y := ymax
end
if BOTTOM in outcodeOut then
begin {Divide line at bottom of clip rectangle}
x := x0 + (x1 - x0) * (ymin - y0) / (y1 - y0);
y := ymax
end
else if RIGHT in outcodeOut then
begin {Divide line at right edge of clip rectangle}
y := y0 + (y1 - y0) * (xmax - x0) / (x1 - x0);
x := xmax
end
else if LEFT in outcodeOut then
begin {Divide line at left edge of clip rectangle}
y := y0 + (y1 - y0) * (xmin - x0) / (x1 - x0);
x := xmin
end;
{Now we move outside point to intersection point to clip, and get ready for next pass.}
if (outcodeOut = outcode0) then
begin
x0 := x; y0 := y; CompOutCode(x0,y0,outcode0)
end
else
begin
x1 := x; y1 := y; CompOutCode(x1,y1,outcode1);
end
end {subdivide}
until done;
if accept then MidpointLineReal(x0,y0,x1,y1,value) {Version for real coordinates}
end; {CohenSutherlandLineClipAndDraw}Sv:Klurig Mattegrej....
Har läst igenom den första länken flera gånger och skrivit om koden till mer lättläslig men jag får inte ihop hur han räknar...
Måste man inte räkna ut vinklar på linjerna och se var de skär MAX/MIN kanten?? Och något sådant har han ju inte med....Sv: Klurig Mattegrej....
Nu när jag äntligen fick in KODen här på forumet såg jag hur de tänkte tror jag.. Men är inte säker... Måste hem och testa på datorn efter jobbet...