How to train a machine learning model if there is a relationship between two different data points?
This is a very broad question and really depends on the type of data you have and how it is distributed.

There are many types of classifiers that you can use.
This link, from scikit-learn shows you a comparison between many algorithms that could help you choose which one to pick.

I understand you want to write the algorithm yourself, but I would recommend you looking at scikit-learn.org and trying out different algorithms. Once you have tried and wanted to implement yourself, you can have a look at blogs, like this one, which explains in details the intrinsic works of models, like k-nearest-neighbours for example.