# PureBasic Forum

 It is currently Thu Jun 04, 2020 7:27 pm

 All times are UTC + 1 hour

 Page 1 of 2 [ 19 posts ] Go to page 1, 2  Next
 Print view Previous topic | Next topic
Author Message
 Post subject: Vector Drawing Change coordinates systemPosted: Fri Nov 08, 2019 10:24 am
 Always Here

Joined: Fri Oct 23, 2009 2:33 am
Posts: 6189
Location: Wales, UK
It's relatively easy to draw shapes, especially when each path line is relative to the previous, in the default coordinate system.

However, I need to import line drawings whose points are defined in a typical CAD coordinate system:

How do I move and 'flip' the default system to work as above, with the points of each line being absolute?

_________________
IdeasVacuum
If it sounds simple, you have not grasped the complexity.

Top

 Post subject: Re: Vector Drawing Change coordinates systemPosted: Fri Nov 08, 2019 10:59 am
 PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3612
Hi IdeasVacuum
Long time no see
But Next could be a start

Code:
If OpenWindow(0, 0, 0, 450, 300, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

If StartVectorDrawing(CanvasVectorOutput(0))

AddPathSegments("M 40 20 L 120 20 L 120 60 L 200 60 L 200 100 L 280 100 L 280 140 L 360 140 L 360 180")
VectorSourceColor(RGBA(255, 0, 0, 255))
StrokePath(5, #PB_Path_RoundCorner)

TranslateCoordinates(30, 30)
AddPathSegments("M 40 20 L 120 20 L 120 60 L 200 60 L 200 100 L 280 100 L 280 140 L 360 140 L 360 180")
VectorSourceColor(RGBA(0, 0, 255, 255))
StrokePath(5, #PB_Path_RoundCorner)
StopVectorDrawing()
EndIf

Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
EndIf

_________________
Egypt my love

Top

 Post subject: Re: Vector Drawing Change coordinates systemPosted: Fri Nov 08, 2019 11:10 am

Joined: Sat Feb 08, 2014 3:26 pm
Posts: 854
I'm not sure, but the main difficulty is that CAD programs put the origin at the bottom left (as in mathematics) and Y in positive value upwards while the PB Vector lib puts the origin at the top left and the positive values to the bottom.

Neither the coordinate translation function nor the mirror function can do this (as I read in the doc)

I think it is therefore necessary to invert Y by setting the maximum value of the drawing area and subtract the value of Y each time.

Maybe with a macro ?
Code:
New_Y = PB_Y - DXF_Y
Where:
PB_Y = bottom left of drawing area
DXF_Y = Value of Y in exported file (DXF ?)

_________________
(English is not my native language, I use an online translator.)

Top

 Post subject: Re: Vector Drawing Change coordinates systemPosted: Fri Nov 08, 2019 11:22 am

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 4728
Location: Germany
And it is possible that the coordinates from the CAD file are in mm or something else, normaly not in pixel.
So you have to transform them.

Have you a small example CAD file?

Top

 Post subject: Re: Vector Drawing Change coordinates systemPosted: Fri Nov 08, 2019 11:33 am

Joined: Sat Feb 08, 2014 3:26 pm
Posts: 854
infratec wrote:
And it is possible that the coordinates from the CAD file are in mm or something else, normaly not in pixel.
So you have to transform them.

Yes, but on this side, this should not be a problem, because in most CAD programs (using the metric system) we work in "drawing units" (dots)
The unit is only given a value at the time of quotation or printing. This allows you to use the same plan for several uses and zoom in and out as much as you want.

_________________
(English is not my native language, I use an online translator.)

Top

 Post subject: Re: Vector Drawing Change coordinates systemPosted: Fri Nov 08, 2019 12:48 pm
 Always Here

Joined: Fri Oct 23, 2009 2:33 am
Posts: 6189
Location: Wales, UK
Hi Guys

Yeah - I was hopeful about the coordinate system but if that's a no, I shall invert all the Y values on import. I already have to swap axis in the OpenGL Gadget, which surprised me given that nearly all CAD-CAM programs use OpenGL.

The Vector Lib does allow different units, including mm. For the purposes of displaying the shape and it's vicinity to other shapes, pixels are fine.

_________________
IdeasVacuum
If it sounds simple, you have not grasped the complexity.

Top

 Post subject: Re: Vector Drawing Change coordinates systemPosted: Fri Nov 08, 2019 4:09 pm

Joined: Sat Feb 08, 2014 3:26 pm
Posts: 854
IdeasVacuum wrote:
Yeah - I was hopeful about the coordinate system but if that's a no, I shall invert all the Y values on import.

Do not invert the Y values (you would get a mirror, so the text (cotations) would be unusable)
Subtract all Y values from the Y value at the bottom of the drawing area

New_Y = Heigh_of_drawing_area - Y

Quick and dirty exemple
Code:
; SVG to PB Vector Drawing

; How exported CAO look like (in SVG)
; Rectangular triangle exported in SVG by GCAD
; 90° angle at bottom left (at 0.0)
; |\
; | \
; +--  I'm poor in ascii art :'(
;
; <?xml version="1.0" encoding="UTF-8"?>
; <!-- 2019-11-08 14:51:23 Generated by QCAD SVG Exporter -->
; <svg width="30" height="30" viewBox="0 -30 30 30" version="1.1" xmlns="http://www.w3.org/2000/svg" style="stroke-linecap:round;stroke-linejoin:round;fill:none">
;     <g transform="scale(1,-1)">
;         <!-- Ligne -->
;         <path d="M0,0 L30,0 " style="stroke:#000000;stroke-width:0.25;"/>
;         <!-- Ligne -->
;         <path d="M30,0 L0,30 " style="stroke:#000000;stroke-width:0.25;"/>
;         <!-- Ligne -->
;         <path d="M0,30 L0,0 " style="stroke:#000000;stroke-width:0.25;"/>
;     </g>
; </svg>

; SVG: x, y
;  0,0  to 30,0
; 30,0  to  0,30
;  0,30 to  0,0

EnableExplicit

Structure Type_Line
X_From.i
Y_From.i
X_To.i
Y_To.i
EndStructure
NewList Line_XY.Type_Line()

Restore Draw:

Define i
For i = 1 To 3
With Line_XY()
EndWith
Next

OpenWindow(0, 0, 0, 540, 540, "CAD 2 PB Vector", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

If StartVectorDrawing(CanvasVectorOutput(0))
TranslateCoordinates(10, 10)
FirstElement(Line_XY())
; Origine PB
ForEach Line_XY()
With Line_XY()
MovePathCursor(\X_From, \Y_From)
EndWith
Next
VectorSourceColor(RGBA(0, 0, 255, 255))
StrokePath(3)

; Corrected
Define New_Y = 500
FirstElement(Line_XY())
ForEach Line_XY()
With Line_XY()
MovePathCursor(\X_From, New_Y - \Y_From)
EndWith
Next
VectorSourceColor(RGBA(255, 0, 0, 255))
StrokePath(3)

StopVectorDrawing()
EndIf

Repeat
Define Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow

End

DataSection
Draw:
Data.i  0,    0, 300, 0
Data.i 300,   0,   0, 300
Data.i  0,  300,   0, 0
EndDataSection

(In blue the drawing as PB traces it with the exported values and in red the rectified drawing)

I didn't test with text, but it should stay straight and wouldn't mirror.

_________________
(English is not my native language, I use an online translator.)

Top

 Post subject: Re: Vector Drawing Change coordinates systemPosted: Fri Nov 08, 2019 4:49 pm

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 3275
Location: Boston, MA
IdeasVacuum wrote:
The Vector Lib does allow different units, including mm. For the purposes of displaying the shape and it's vicinity to other shapes, pixels are fine.
This is a BIG no no if you are dealing with accuracy and object intersections. You must use integer math and/or scale your drawing units way above the limited pixels within the viewable area.
If you are just attempting a gross view, then pixels are fine.

_________________
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum

Top

 Post subject: Re: Vector Drawing Change coordinates systemPosted: Fri Nov 08, 2019 5:42 pm

Joined: Fri May 12, 2006 6:51 pm
Posts: 2487
Location: Germany
If you don't have to write text, FlipCoordinatesY(...) should work.
The angles are also correct according to flip coordinates.

Update v0.2

Update v0.4

Update v0.5
- Bugfix DrawVectorText.

Code:
;-TOP
; Flip Y-Coordinates by mk-soft, v0.6

EnableExplicit

Global _IsFlipped_, _OldY_.d

Macro _PB_(Function)
Function
EndMacro

Macro BeginFlipCoordinates()
FlipCoordinatesY(VectorOutputHeight() * 0.5) : _IsFlipped_ = #True
EndMacro

Macro EndFlipCoordinates()
_PB_(ResetCoordinates)() : _IsFlipped_ = #False
EndMacro

Macro ResetCoordinates(_Value_=#PB_Coordinate_User)
_PB_(ResetCoordinates)(_Value_) : If _IsFlipped_ : FlipCoordinatesY(VectorOutputHeight() * 0.5, _Value_) : EndIf
EndMacro

Macro DrawVectorText(_Text_)
If _IsFlipped_
_OldY_ = PathCursorY()
FlipCoordinatesY(_OldY_) : _PB_(DrawVectorText)(_Text_) : FlipCoordinatesY(_OldY_)
Else
_PB_(DrawVectorText)(_Text_)
EndIf
EndMacro

Define i

If OpenWindow(0, 0, 0, 450, 300, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

If StartVectorDrawing(CanvasVectorOutput(0))
VectorFont(FontID(0))

MovePathCursor(10, 10)
DrawVectorText("Flip Coordinates Example")

BeginFlipCoordinates()

; Part 1
ResetCoordinates()

AddPathSegments("M 40 20 L 120 20 L 120 60 L 200 60 L 200 100 L 280 100 L 280 140 L 360 140 L 360 180")
VectorSourceColor(RGBA(255, 0, 0, 255))
StrokePath(5, #PB_Path_RoundCorner)

MovePathCursor(10, 30)
RotateCoordinates(10, 30, 45)
DrawVectorText("Red Line")

;Part 2
ResetCoordinates()

TranslateCoordinates(50, 50)

AddPathSegments("M 40 20 L 120 20 L 120 60 L 200 60 L 200 100 L 280 100 L 280 140 L 360 140 L 360 180")
VectorSourceColor(RGBA(0, 0, 255, 255))
StrokePath(5, #PB_Path_RoundCorner)

MovePathCursor(10, 30)
RotateCoordinates(10, 30, 45)
DrawVectorText("Blue Line")

; Part 3
ResetCoordinates()

VectorSourceColor(RGBA(255, 0, 0, 255))
StrokePath(5, #PB_Path_RoundCorner)

VectorSourceColor(RGBA(0, 0, 255, 255))
StrokePath(5, #PB_Path_RoundCorner)

; Part 4
ResetCoordinates()

VectorSourceColor(RGBA(64, 64, 64, 255))
For i = 0 To 260 Step 20
MovePathCursor(420, i + 20)
DrawVectorText(Str(i))
Next

EndFlipCoordinates()

StopVectorDrawing()
EndIf

Repeat
Define Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
EndIf

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu

Last edited by mk-soft on Sat Nov 09, 2019 12:25 pm, edited 7 times in total.

Top

 Post subject: Re: Vector Drawing Change coordinates systemPosted: Fri Nov 08, 2019 7:52 pm
 Always Here

Joined: Fri Oct 23, 2009 2:33 am
Posts: 6189
Location: Wales, UK
Thanks for all the responses guys, I will run some tests to see what works best with my data - which is always only lines, no text etc.

_________________
IdeasVacuum
If it sounds simple, you have not grasped the complexity.

Top

 Post subject: Re: Vector Drawing Change coordinates systemPosted: Fri Nov 08, 2019 8:08 pm

Joined: Fri May 12, 2006 6:51 pm
Posts: 2487
Location: Germany
I once extended the macros so that you can switch between BeginFlip and... and EndFlip... can also perform a ResetCoordinates.

P.S. DrawVectorText works now too

P.P.S. Update example

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu

Top

 Post subject: Re: Vector Drawing Change coordinates systemPosted: Sun Nov 10, 2019 11:13 pm
 Always Here

Joined: Fri Oct 23, 2009 2:33 am
Posts: 6189
Location: Wales, UK
... It's just not working for me. The data arrives as pairs of points (the line end points), in no particular order. It would be great if there was a draw line function, I can get my head round that

Simple Example
In PB coordinates, the Origin Point (near paper bottom left corner) must be @ X200 Y800

The line points, in World coordinates, where +X axis at the Origin is left, +Y axis is Up and Origin Point is X0, Y0
(@ The Paper Origin):

Line01 Pt01 101.1176, 0.0000 Pt02 756.6081, 0.0000
Line02 Pt01 739.8827, 106.6284 Pt02 10.0000, 167.9937
Line03 Pt01 10.0000, 167.9937 Pt02 101.1176, 0.0000
Line04 Pt01 739.8827, 106.6284 Pt02 756.6081, 0.0000

I think the 2D Drawing Lib, or GDI+, might be an easier way to do this.

What I would really like to do is specify the coordinate system, that would make everything much easier.

_________________
IdeasVacuum
If it sounds simple, you have not grasped the complexity.

Top

 Post subject: Re: Vector Drawing Change coordinates systemPosted: Mon Nov 11, 2019 1:50 am
 Always Here

Joined: Fri Oct 23, 2009 2:33 am
Posts: 6189
Location: Wales, UK
Well, I have found it's much easier if:

1) Use TranslateCoordinates() to set the Origin @ paper bottom left
2) Simply invert the Y values.

_________________
IdeasVacuum
If it sounds simple, you have not grasped the complexity.

Top

 Post subject: Re: Vector Drawing Change coordinates systemPosted: Mon Nov 11, 2019 6:32 am
 PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3612
Hi IdeasVacuum

Code:
Procedure lline(x1,y1,x2,y2)
ResetCoordinates()
FlipCoordinatesY(90)
MovePathCursor(x1,y1)
EndProcedure

If OpenWindow(0, 0, 0, 800, 200, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
;Line01 Pt01 101.1176, 0.0000 Pt02 756.6081, 0.0000
;Line02 Pt01 739.8827, 106.6284 Pt02 10.0000, 167.9937
;Line03 Pt01 10.0000, 167.9937 Pt02 101.1176, 0.0000
;Line04 Pt01 739.8827, 106.6284 Pt02 756.6081, 0.0000
If StartVectorDrawing(CanvasVectorOutput(0))
lline(101,0,756,0)
lline(739,106,10,167)
lline(10,167,101,0)
lline(739,106,756,0)
VectorSourceColor(RGBA(255, 0, 0, 255))
StrokePath(1)

StopVectorDrawing()
EndIf

Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
EndIf

_________________
Egypt my love

Top

 Post subject: Re: Vector Drawing Change coordinates systemPosted: Mon Nov 11, 2019 9:52 am

Joined: Sat Feb 08, 2014 3:26 pm
Posts: 854
Version with subtraction of coordinates

(Save file in same Dir as data.txt before run)
Code:

EnableExplicit

Enumeration
#hFile
#Win
#Canvas
EndEnumeration

Structure Type_Line
x1.l
y1.l
x2.l
y2.l
EndStructure
NewList vLine.Type_Line()

Define Data_File\$ = "Datas.txt" ; (see content sample below)

; Line01 Pt01 101.1176, 0.0000 Pt02 756.6081, 0.0000
; Line02 Pt01 739.8827, 106.6284 Pt02 10.0000, 167.9937
; Line03 Pt01 10.0000, 167.9937 Pt02 101.1176, 0.0000
; Line04 Pt01 739.8827, 106.6284 Pt02 756.6081, 0.0000

OpenWindow(#Win, 0, 0, 800, 300, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
CanvasGadget(#Canvas, 10, 10, WindowWidth(#Win) - 20, WindowHeight(#Win) - 20)

; --- Load datas from file
If Not OpenFile(#hFile, Data_File\$)
End
EndIf
While Not Eof(#hFile)
With vLine()
\x1 =         Val(StringField(Tmp_Line\$, 3, " "))
\y1 = New_Y - Val(StringField(Tmp_Line\$, 4, " "))
\x2 =         Val(StringField(Tmp_Line\$, 6, " "))
\y2 = New_Y - Val(StringField(Tmp_Line\$, 7, " "))
EndWith
Wend
CloseFile(#hFile)

If StartVectorDrawing(CanvasVectorOutput(#Canvas))
With vLine()
ForEach vLine()
MovePathCursor(\x1, \y1)
Next
EndWith
VectorSourceColor(RGBA(0, 0, 255, 255))
StrokePath(1)
StopVectorDrawing()
EndIf

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

Data.txt
Code:
Line01 Pt01 101.1176, 0.0000 Pt02 756.6081, 0.0000
Line02 Pt01 739.8827, 106.6284 Pt02 10.0000, 167.9937
Line03 Pt01 10.0000, 167.9937 Pt02 101.1176, 0.0000
Line04 Pt01 739.8827, 106.6284 Pt02 756.6081, 0.0000

It remains to test what will run fastest with a lot of data ?

_________________
(English is not my native language, I use an online translator.)

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 2 [ 19 posts ] Go to page 1, 2  Next

 All times are UTC + 1 hour

#### Who is online

Users browsing this forum: Peace and 26 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ PureBasic    Coding Questions    Game Programming    3D Programming    Assembly Programming    The PureBasic Editor    The PureBasic Form Designer    General Discussion    Feature Requests and Wishlists    Tricks 'n' Tips Bug Reports    Bugs - Windows    Bugs - Linux    Bugs - Mac OSX    Bugs - IDE    Bugs - Documentation OS Specific    AmigaOS    Linux    Windows    Mac OSX Miscellaneous    Announcement    Off Topic Showcase    Applications - Feedback and Discussion    PureFORM & JaPBe    TailBite